How to access parent's DataContext from a UserControl

后端 未结 4 2074
旧时难觅i
旧时难觅i 2020-12-10 14:25

I need to access the container\'s DataContext from a UserControl (a grid containing textboxes and a listbox: I need to insert items in this list box) that I created in WPF:

4条回答
  •  失恋的感觉
    2020-12-10 14:31

    Normally the DataContext will be inherited, just do not explicitly set it on the UserControl and it will get it from its parent. If you have to set it you could still use the Parent property to get the parent, which you then can safe-cast to a FrameworkElement and if it is not null you can grab its DataContext.

提交回复
热议问题