Bindings
You bind in XAML using the {Binding}
markup extension. By using bindings (assuming you've implemented change notifications) any changes to the data context will automatically be updated in the control.
By default a binding binds to a property on the DataContext
, e.g.:
<!-- Binds to the TextBlock's DataContext.Name property -->
<TextBlock Text="{Binding Name}"/>
<!-- Which is the same as ('Path' is optional) -->
<TextBlock Text="{Binding Path=Name}"/>
An empty binding binds to DataContext itself
<!-- Binds to the TextBlock's DataContext property -->
<TextBlock Text="{Binding}"/>
<!-- Which is the same as -->
<TextBlock Text="{Binding .}"/>
We call the property on the control the binding target and the property on the DataContext
the binding source.
Binding Path
The binding path above can be a single property, or it can be a chain of properties. For example if the object assigned to the DataContext
has a Student
property, and the value of this property has a Name
, you can bind to the student name using:
<TextBlock Text="{Binding Student.Name}"/>
You can also include array/list indexers in binding paths:
<TextBlock Text="{Binding Students[0].Name}"/>
Binding Modes
You can change the behavior of a {Binding}
by specifying a binding Mode
:
<TextBlock Text="{Binding Name, Mode=OneTime}">