Binding to Controls
As well as binding to a control's DataContext
you can also bind to other controls.
к сведению
Note that when you do this, the binding source is to the control itself not the control's DataContext
. If you want to bind to the control's DataContext
then you'll need to specify that in the binding path.
Binding to a named control
If you want to bind to a property on another named control, you can use the control name prefixed by a #
character.
<TextBox Name="other">
<!-- Binds to the Text property of the "other" control -->
<TextBlock Text="{Binding #other.Text}"/>
This is the equivalent to the long-form binding that will be familiar to WPF and UWP users:
<TextBox Name="other">
<TextBlock Text="{Binding Text, ElementName=other}"/>
Avalonia supports both syntaxes but the short-form #
syntax is less verbose.
Binding to an Ancestor
You can bind to the logical parent of the target using the $parent
symbol:
<Border Tag="Hello World!">
<TextBlock Text="{Binding $parent.Tag}"/>
</Border>