I have a UserControl like the following:
<UserControl>
<Expander>
<Expander.HeaderTemplate>
<DataTemplate>
<Grid HorizontalAlignment="{Binding Path=HorizontalAlignment, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}, Mode=OneWayToSource}">
<TextBlock Text="{Binding Path=Service, Mode=TwoWay}"/>
</Grid>
</DataTemplate>
</Expander.HeaderTemplate>
</Expander>
</UserControl>
I want to bind the Text property of the TextBlock control to a property of my UserControl class, like for example:
public string Service
{ get; set; }
How can I do?
Try setting the
DataContextto theUserControlso you can access the propertiesIn this situation I have named the
UserControl“UI” (Name="UI") so you can bind usingElementNameText="{Binding ElementName=UI, Path=Service}"Example:
Code:
I have implemented
INotifyPropertyChangedthis will allow the UI to be updated whenServicestring changesResult: