How to bind a child user control's data context in the parent

TextBlock  Text="{Binding ChildGroupName,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type
UserControl}},UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True,Mode=TwoWay}"
TargetUpdated="OnTextUpdated"/> 

Here ChildGroupName is a child control datacontext property. i want to bind ChildGroupName values to parent window.


You cannot use FindAncestor to data bind to a descendant's data... the clue is in its name. If the child UserControl is defined in the same XAML as the parent, then you can provide it with a name and then use the Binding.ElementName Property to data bind to its properties:

<TextBlock Text="{Binding ChildPropertyName, ElementName=NameOfChildUserControl, 
    UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, Mode=TwoWay}" 
    TargetUpdated="OnTextUpdated" />
链接地址: http://www.djcxy.com/p/44624.html

上一篇: WPF Combobox绑定和SelectedValue与SelectedValuePath

下一篇: 如何绑定父级中的子级用户控件的数据上下文