Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wpf

xaml

<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.

like image 778
Manikandan Avatar asked Mar 12 '15 13:03

Manikandan


1 Answers

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" />
like image 153
Sheridan Avatar answered Nov 15 '22 16:11

Sheridan