Lets look at the following quickly:
· Creating bindings in code
· Creating bindings in XAML
· Static Resources
· Binding Direction
· Update Frequency
· Hierarchical data binding
Creating bindings in code
The System.Windows.Data.Binding class is what synchronizes two properties:
· Source is the object where the data is coming from.
· Path is the property to retrieve the value from.
· Target identifies the instance and the property the data is going to.
As an example, we create a simple class called Customers having a few properties:
class Customer
{
string Name;
string Address;
string Phone;
…
}
Now, to get the properties bound to WPF controls using code, we need to create the binding object and associate it with the desired control.
Customer customer=New Customer(“Foo”,….);
Binding binding=new Binding();
Binding.Source=customer;
Binding. Path=new PropertyPath(“Name”);
MyTextBox.SetBinding(TextBox.TextProperty,binding);
In the context of the above example, source object and property can be any CLR object while the target property must be a DependencyProperty.
Creating bindings in XAML
Bindings can be seamlessly created in XAML, as in code. Once again, binding needs a target DependencyProperty, while source typically is a static resource or is set in code-behind.
As an example, let us look at the following snippet, where we bind a customer’s name to the Text property of a TextBox control:
<StackPanel>
<StackPanel.Resources>
<me.Customer x:Key=”customer” Name=”Foo”…../>
</StackPanel.Resources>
<Label>Customer Name: </Label>
<TextBox>
< TextBox .Text>
<Binding Source=”{StaticResource customer}” Path=”Name”/>
</TextBox .Text>
</TextBox>
<StackPanel>
As a shorthand notation, the following would have also accomplished the same objective:
<TextBox Name=”tb” Text=”{Binding Source={StaticResource customer} , Path=Name}”/>
The real flexibility of XAML binding is realized when we tie the properties of different elements together.
As an example, we try to tie up the opacity of an image to the value of a slider control:
<StackPanel>
<Slider Name=”sl” Minimum=”0” Maximum=”1” Width=”100” Value=”1”>
</Slider>
<Image Source=”i1.jpg” Width=”100” Opacity=”{Binding ElementName=s1 , Path=Value}”>
</Image>
<StackPanel>
The DataContext Property provides a binding source which is inherited across all children of a given control. It is typically set in code behind, but here we shall look into a XAML based example:
<Grid>
<Grid.DataContext>
<me.Customer x:Key=”customer” Name=”Foo”…../>
</Grid.DataContext>
<Label Content=”Name:”>
<Textbox Text=”{Binding Path=Name}”>
</Grid>
In the above situation, the TextBox directly uses the grid’s DataContext to locate its binding source.
Binding Direction
Binding can control how data is transferred using the Mode property.
The possible values of this property are:
· OneTime - copy value once
· OneWay - copy value from source to target only
· TwoWay - copy value from source to target and vice-versa
The default mode depends on the target DependencyProperty and whether the source property is read-only. For example, the following snippet shows a one way binding:
<TextBox Name=”tb” Text=”{Binding Source={StaticResource customer} , Path=Name, Mode=OneWay}/>
Update Frequency
Updates can be controlled using the UpdateSourceTrigger property.
The possible values of this property are:
· LostFocus - copies the values when the focus is lost on source
· PropertyChanged - copies the values when the source value changes
· Explicit – copies the values only when Binding.UpdateSource is called
<TextBox Name=”tb” Text=”{Binding Source={StaticResource customer} , Path=Name, UpdateSourceTrigger =PropertyChanged }” />
Hierarchical data binding
Look into this post by John, simple and self-explainatory:
http://joshsmithonwpf.wordpress.com/2007/05/05/binding-a-treeview-to-a-dataset/