WPF – Different Kind of Binding

How to bind a control using different binding techniques in this article we will discuss about maximum way to bind a control of User Control. Binding control to object, Binding control to static resources, binding control to an xml file or data source and dynamically binding in code.

Bind Control with object

To bind a control with object we need following things

First: Control

<Expander Header="{Binding CustomerName}" IsExpanded="False" HorizontalAlignment="Left" Margin="10,10,0,0" Name="expander1" VerticalAlignment="Top" Background="Wheat" Width="470" >
<Border BorderBrush="Black" BorderThickness="1" >
<StackPanel Height="300" Width="470"/ >
</Border >
</Expander >

Second: Object

public class Customer : INotifyPropertyChanged
public int ID { get; set; }
public string CustomerName { get; set; }
public event PropertyChangedEventHandler PropertyChanged;

Third: Binding

Customer customer = new Customer {ID = 1, CustomerName = "Tony John"};
expander1.DataContext = customer;

Binding Control with Static Resources

To Bind some property of control to resources we need following things like

First : Resources

<Window.Resources gt;
<SolidColorBrush x:Key="ExpandarBGColor" Color="Wheat" />
<System:String x:Key="CustomerName">Tony John</Window.Resources >

Second : Control and key of resource

< Expander Background="{StaticResource ExpandarBGColor}"
Header="{StaticResource CustomerName}" Width="470" >
</Expander >

Note: Here we can also create resource dictionary and put this resources in resources dictionary and then we can call it and by using or by giving reference of the file wpf engine will find the resources on the basis of key provided by us.

Binding Control With Xml

To bind an control with xml we need following things first Data Provider or object i.e. either can be an xml file or can Xml Data Provider with data and a control to bind it.

< XmlDataProvider x:Key="Customers1" XPath="/Customers" >
< x:XData >
lt;Customers gt;
<Customer id="1" name="tony"/>
<Customer id="2" name="anil"/>
<Customer id="3" name="pk"/>
</Customers >
</x:XData >
</XmlDataProvider >

< ListBox Width="248" Height="56"
ItemsSource="{Binding Source={StaticResource Customers1},XPath=Customer/@id}" >
</ListBox >

Dynamic Binding

Customer customer = new Customer { ID = 1, CustomerName = "Tony 123 John" };
Binding binding = new Binding("CustomerName");
binding.Source = customer;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
txtBox1.SetBinding(TextBlock.TextProperty, binding);

Note : The Textbox nature is something different like default update source trigger is LostFocus but most of the time we need it as PropertyChanged to reflect the value in the current control.


Author: Rathimaran04 Aug 2014 Member Level: Silver   Points : 0

Nice Article friends....

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: