Binding issue with WPF user control

前端 未结 2 682
离开以前
离开以前 2021-01-20 02:09

Here\'s what I essentially want. A UserControl with a TextBlock whose Text property is binded to the Prop property of the

2条回答
  •  一生所求
    2021-01-20 02:49

    The reason it doesn't work is that binding paths are relative to the DataContext, not to the (parent) control.

    This is why you can fix this by setting the RelativeSource; in that case the binding path uses the RelativeSource as the starting point of finding the property.

    Another way to solve it is by naming the parent and setting the ElementName of the binding.

    The MVVM way of doing this is adding a property to a ViewModel class, set the parent control's DataContext to an instance of the ViewModel and binding both the parent control and the client control to that property.

提交回复
热议问题