Saturday, 11 May 2013

Binding in WPF | WPF Tutorial pdf

WPF puts the concept of Binding further and introduced new features, so that we could use the Binding feature extensively. Binding establishes the connection between the application and the business layers. If you want your application to follow strict design patter rules, Data Binding concept will help you to achieve that.
We will look into greater detail with how to do that in a while.
In WPF we can bind two Properties, One Property and one Dependency Property, two Dependency Properties etc. WPF also supports Command Binding. Lets discuss how to implement them in detail.

Binding can be classified into few Types :
DataBinding / Object Binding
The most important and primary binding is Databinding. WPF introduces objects like Object Data Provider and XMLDataProvider to be declared into XAML to enhance the capability of object binding. DataBinding can be achieved by several ways. As shown by Adan in his blog we can make use of Binding capabilities by employing either
XAML, XAML and C#, and C# itself. So WPF is flexible enough to handle any situation.
<TextBox x:Name="txtName" />
<TextBlock Text="{Binding ElementName=txtName, Path=Text.Length}" />
In the above situation, I have shown the most basic usage of Binding. The Text property of TextBlock is bound with the TextBox txtName so that whenever you enter something on the TextBox during runtime, the TextBlock will show the length of the string.
As a Markup Extension binding is actually a Class with properties. Here we specified the value of the property ElementName and Path. The Element Name ensures that the object that the property belongs to. Path determines the property path which the object needs to look into.
You can use ObjectDataProvider to handle data in your XAML easily.
ObjectDataProvider can be added as Resource and later on can be referenced using StaticResource.
Lets see the code below :
<StackPanel Orientation="Vertical">
<StackPanel.Resources>
<ObjectDataProvider ObjectType="{x:Type m:StringData}"
x:Key="objStrings" MethodName="GetStrings"/>
</StackPanel.Resources>
<ListBox Name="lstStrings" Width="200" Height="300" ItemsSource="{Binding
Source={StaticResource objStrings}}" />
Just as shown above the ObjectType will get a Type, which is the internal class structure for which the Method GetStrings will be called for. From the ListBox, I have referenced the Object using StaticResource.
Now in the code you can declare a class
public class StringData
{
ObservableCollection<String> lst= new ObservableCollection<String>();
public StringData()
{
lst.Add("Abhishek");
lst.Add("Abhijit");
lst.Add("Kunal");
lst.Add("Sheo");
}
public ObservableCollection<String> GetStrings()
{
return lst;
}
}
So you can see the list been populated with the strings.
Why ObservableCollection , the INotifyPropertyChanged,

INotifyCollectionChanged?
Now as you can see I have used ObvervableCollection. This is important.
ObservableCollection sends automatic notification when a new item is inserted. Thus notifies the ListBox to update the list. So if you place a button,which inserts some data in the Observable Collection, the Binding will automatically be notified by the collection and hence update the  collection automatically. You dont need to manually insert the same in the ListBox.
WPF Binding generally needs to be notified when it is modified. The interfaces INotify Property Changed and INotify Collection Changed are needed to update the UIElement which is bound with the data. So if you are crating a property which needed to update the UI when the value of it is modified, the minimum requirement is to implement the same from INotifyPropertyChanged, and for collection (like ItemsSource), it needs to implement INotifyCollectionChanged. ObservableCollectionitself implements INotifyCollectionChanged, so it has support to update the control whenever new item is inserted to the list or any old item is removed from the string.
I have already discussed the two in detail in an article :
Change Notification for Objects and Collection

XML Binding
Similar to Object binding, XAML also supports XML binding. You can bind the data coming from XMLDataProvider easily using built in properties like XPath in Binding class definition. Lets look into the code :
<TextBlock Text="{Binding XPath=@description}"/>
<TextBlock Text="{Binding XPath=text()}"/>
So if you are in the node XYZ, the InnerText can be fetched using text() property. The @ sign is used for Attributes. So using XPath you can easily handle your XML.
If you want to read more about XML binding check:
XML Binding in WPF

Importance of DataContext
You might wonder why I have took context of DataContext while I am talking about WPF Bindings. DataContext is actually a Dependency property. It points to Raw Data such that the object that we pass as DataContext will inherit to all its child controls. I mean to say if you define the DataContext for a Grid, then all the elements that are inside the Grid will get the same DataContext.
<Grid DataContext="{StaticResource dtItem}">
<TextBox Text="{Binding MyProperty}" />
</Grid>
Here as I defined DataContext for the Grid, the TextBox inside the grid can refer to the property MyProperty as the dtItem object will be automatically inherited to all its child elements. While using Binding, DataContext is the most important part which you must use.

No comments: