Subscribe to Subscribers

Online Members

baskar
More...

Resources » .NET programming » WPF

WPF – Different Kind of Binding


Posted Date:     Category: WPF    
Author: Member Level: Gold    Points: 30


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.





Did you like this resource? Share it with your friends and show your love!


Responses to "WPF – Different Kind of Binding"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to do Animation in WPF
    Previous Resource: Example of Graphics in WPF
    Return to Resources
    Post New Resource
    Category: WPF


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Dynamic Binding with control  .  Binding Control With Static Resources  .  Binding Control with Xml  .  

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.