<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>WPF</title>
        <link>http://geekswithblogs.net/Shuvo/category/9485.aspx</link>
        <description>Windows Presentation Foundation</description>
        <language>en-US</language>
        <copyright>Tanveer-Ibn-Haresh</copyright>
        <managingEditor>ta_haresh@hotmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>WPF Two way databinding explained</title>
            <link>http://geekswithblogs.net/Shuvo/archive/2009/02/16/wpf-two-way-databinding-explained.aspx</link>
            <description>Many get confused over the two way databinding concept. This is usually  done by implementing INotiyPropertyChanged interface.You can find many examples with INotiyPropertyChanged if you google for it.  Here I am presenting a very basic example of WPF databinding to show its advantages. &lt;br /&gt;
&lt;br /&gt;
Create a WPF Application called "TwoWayDataBinding". You have app.xaml and Window1.xaml. Add one class called Customer to the project. The code for the class is bellow.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;using System;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; using System.Collections.Generic;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; using System.Linq;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; using System.Text;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; namespace TwoWayDataBind&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; {&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     class Customer&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     {&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         private string m_name; &lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         public string Name&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         {&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             get { return m_name; }&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             set { m_name = value; }&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         }&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         private string m_State;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         public string State&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         {&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             get { return m_State; }&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             set { m_State = value; }&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         }&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     }&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
We have two string  properties called  name and state in the customer class. We are going to use this class as the datasource for our  WPF form.&lt;br /&gt;
&lt;br /&gt;
Now look at the XAML code for the Window1.xaml file. The explanations will follow.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;Window x:Class="TwoWayDataBind.Window1"&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     xmlns:myapp="clr-namespace:TwoWayDataBind"&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     Title="Window1" Height="300" Width="300"&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     &amp;lt;Window.Resources&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         &amp;lt;myapp:Customer x:Key="Cust2" Name="Hansen" State="Arizona"/&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     &amp;lt;/Window.Resources&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     &amp;lt;Grid x:Name="Grid1"  DataContext="{StaticResource Cust2}"&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         &amp;lt;Grid.RowDefinitions&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             &amp;lt;RowDefinition Height="Auto"/&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             &amp;lt;RowDefinition Height="Auto"/&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             &amp;lt;RowDefinition Height="Auto"/&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         &amp;lt;/Grid.RowDefinitions&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         &amp;lt;Grid.ColumnDefinitions&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             &amp;lt;ColumnDefinition Width="Auto" /&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             &amp;lt;ColumnDefinition Width="*"/&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         &amp;lt;/Grid.ColumnDefinitions&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         &amp;lt;TextBlock Grid.Column="0" Grid.Row="0"&amp;gt;Name:&amp;lt;/TextBlock&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         &amp;lt;TextBlock Grid.Column="0" Grid.Row="1"&amp;gt;State:&amp;lt;/TextBlock&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         &amp;lt;TextBox x:Name="TextBox1" Grid.Column="1" Grid.Row="0" Text="{Binding Path=Name}"&amp;gt;&amp;lt;/TextBox&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         &amp;lt;TextBox x:Name="TextBox2" Grid.Column="1" Grid.Row="1" Text="{Binding Path=State}"&amp;gt;&amp;lt;/TextBox&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         &amp;lt;Button Grid.Column=" 1" Grid.Row="2"   Name="button1" Click="OnClicked"&amp;gt;Control To Context&amp;lt;/Button&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;        &lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     &amp;lt;/Grid&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; &amp;lt;/Window&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In the first  line the class Name for the Window1 form is specified.&lt;br /&gt;
&lt;br /&gt;
Next, there are two XML namespace declarations. The first declaration maps the overall  Windows Presentation Foundation (WPF) namespace as the default:&lt;br /&gt;
&lt;p&gt;The second declaration maps a separate Extensible Application Markup Language  (XAML) namespace, mapping it (typically) to the &lt;span&gt;&lt;span class="input"&gt;x:&lt;/span&gt;&lt;/span&gt; prefix. &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Coming on to the next part, you can map XML namespaces to assemblies using a series of tokens within an  &lt;span&gt;&lt;span class="input"&gt;xmlns&lt;/span&gt;&lt;/span&gt; prefix declaration, similar to how  the standard WPF and XAML namespaces are mapped to prefixes.We are mapping a xml namespace myapp with the TwoWayDataBind Assembly . This will be  used in the next segment.&lt;/p&gt;
&lt;p&gt;In the next section,we are declaring a resource dictionary. Microsoft defines these dictionaries as "Resource dictionaries that can be defined completely or partially in Extensible  Application Markup Language (XAML) are typically created as a property element,  and are typically on the root element for any individual page or for the  application. Placing the resource dictionary at this level makes it easier to  find from individual child elements in the page (or from any page, in the  application case)". We are declaring a resource for  window element, so that all child elements of the window can access it. You can also define resources for page,grid and other elements. We used the xml namespace myapp, that we mapped to our application assembly section. We are accesing the customer class of the TwoWayBindingAssmbly with myapp:Customer. We are defining a key for the resource, which will be used for accessing it from other elements in the form. We are also specifying values for the two customer properties.&lt;/p&gt;
&lt;p&gt;Next we define the grid element for the WPF form. Look at the part  DataContext="{StaticResource Cust2}. This is the important part for defining the databinding for our form. DataContext is a dependency property. It gets or sets the data context for an element when it participates in data  binding. So are defining a datacontext for the Grid element here.We are defining the datacontext using the StaticResource. Static Resource provides a value for any XAML property attribute by looking up a reference to an  already defined resource. That defined resource here is obviously Cust2, which is nothing but our customer class.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;The Next 11 lines defines the form design xaml, as any wpf developer would find it  easy to understand. Coming to the part of our interest :&lt;/p&gt;
&lt;p&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt; &amp;lt;TextBox x:Name="TextBox1" Grid.Column="1" Grid.Row="0" Text="{Binding Path=Name}"&amp;gt;&amp;lt;/TextBox&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         &amp;lt;TextBox x:Name="TextBox2" Grid.Column="1" Grid.Row="1" Text="{Binding Path=State}"&amp;gt;&amp;lt;/TextBox&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   Here we are binding the text properties of the text boxes to the properties of the customer class. MSDN describes WPF databinding as "&lt;br /&gt;
&lt;/p&gt;
&lt;div class="ArticleNormalPara"&gt;To use WPF data &lt;font color="#ffffff" style="background-color: rgb(49, 106, 197);"&gt;binding&lt;/font&gt;, you must always  have a target and a source. The target of the &lt;font color="#ffffff" style="background-color: rgb(49, 106, 197);"&gt;binding&lt;/font&gt; can be any  accessible property or element that is derived from DependencyProperty—an  example is a TextBox control's Text property. The source of the &lt;font color="#ffffff" style="background-color: rgb(49, 106, 197);"&gt;binding&lt;/font&gt; can be any public  property, including properties of other controls, common language runtime (CLR)  objects, XAML elements, ADO.NET DataSets, XML Fragments, and so forth. To help  you get the &lt;font color="#ffffff" style="background-color: rgb(49, 106, 197);"&gt;binding&lt;/font&gt;  right, WPF includes two special providers—the XmlDataProvider and the  ObjectDataProvider."&lt;br /&gt;
&lt;br /&gt;
The syntax we are using here is called attribute syntax. It condenses the data &lt;font color="#ffffff" style="background-color: rgb(49, 106, 197);"&gt;binding&lt;/font&gt; code inside of the Text attribute of the TextBox.  Basically, the &lt;font color="#ffffff" style="background-color: rgb(49, 106, 197);"&gt;Binding&lt;/font&gt; tag gets pulled inside of the curly braces along  with its attributes. "Path" is one of the Four components of Binding class. It gets or sets the path to the binding source property.  So here we are setting the two properties of  the customer class the the Binding source property of the textbox's text propety.&lt;br /&gt;
&lt;br /&gt;
We have a button on the form and it has a click event method defined. Here is the code for the codebehind for the form.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="text-decoration: underline; color: rgb(0, 0, 255); font-weight: bold;"&gt;Window1.xaml.cs&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; using System;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; using System.Collections.Generic;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; using System.Linq;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; using System.Text;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; using System.Windows;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; using System.Windows.Data;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; namespace TwoWayDataBind&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; {&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     /// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     /// Interaction logic for Window1.xaml&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     /// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     public partial class Window1 : Window&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     {&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         public Window1()&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         {&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             InitializeComponent();&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             &lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         }&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         private void OnClicked(object sender, RoutedEventArgs args)&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         {&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             Customer c1=Grid1.DataContext as Customer;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;            &lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             TextBox1.Text = c1.State;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;             &lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;         }&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;        &lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;     }&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt; }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now if you run the project  you will see.&lt;br /&gt;
&lt;img height="304" width="307" alt="" src="/images/geekswithblogs_net/Shuvo/TanveerShuvo/WPF.jpg" /&gt;&lt;br /&gt;
&lt;br /&gt;
So the textbox's are bound to the customer properties. We can see the values of the customer object in the texboxes.So this is one side of the databinding...the target object is getting update by the value of the data source. Now write something in the state textbox and click the button. What do you see? The Name textbox getting populated with the value of the State textBox.&lt;br /&gt;
&lt;br /&gt;
If we look at the code for the button click:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;Customer c1=Grid1.DataContext as Customer;&lt;/span&gt;&lt;br style="color: rgb(0, 0, 255);" /&gt;
&lt;span style="color: rgb(0, 0, 255);"&gt;  TextBox1.Text = c1.State;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
We are not assigning the new value to the customer class or datacontext. As we type  in the State textbox, the dataContext  is getting updated. So when we assign the datacontext's State value to the Name textbox, we see it gets populated with the new value that is entered. Isn't it just fantastic?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;p&gt; &lt;/p&gt; &lt;img src="http://geekswithblogs.net/Shuvo/aggbug/129425.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Tanveer-Ibn-Haresh</dc:creator>
            <guid>http://geekswithblogs.net/Shuvo/archive/2009/02/16/wpf-two-way-databinding-explained.aspx</guid>
            <pubDate>Mon, 16 Feb 2009 18:14:01 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Shuvo/comments/129425.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Shuvo/archive/2009/02/16/wpf-two-way-databinding-explained.aspx#feedback</comments>
            <slash:comments>42</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Shuvo/comments/commentRss/129425.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
