Social Icons

Wednesday, 14 August 2013

Binding Modes in WPF with Example


     In this article will see purpose of Mode property in Data Binding, What are the different Data Binding Modes available in WPF, When to use each of these binding modes etc.


       Mode is the one of the property of the Binding Element. Mode attribute defines the Data Binding Mode which determines how data flows between source and target. 

Four Types of Data Binding Modes available.
  • OneWay
  • OneTime
  • OneWayToSource
  • TwoWay
For understanding Binding Modes briefly let us take an example which contains a ListBox, a TextBlock, a TextBox. ListBox contains List of Colors, Selected value of ListBox is binded to Background and Text dependency properties of TextBlock, and Text property of TextBox.

XAML Code Looks like below.


                If Binding Mode is OneWay means whenever the Source Data is changed then that data is reflected in Target UI. Suppose in the above example we binded the listbox's  SelectedValue.Content to TextBox and TextBlock's Background and Text Properties. So whenever we select another ListBoxItem then that value is Update in Target i.e in TextBox and TextBlock.

OneWay Binding Mode in WPF
OneWay Binding
  Note: If Source Changes then only UI will Updated. Use this only when you want to display read only data in the UI.


       If Binding Mode is OneTime, then it sends data from source to target, but it does this only  when application started or DataContext changes. So in our example change the Mode Attribute value to OneTime and set the ListBox's SelectedIndex to 0 then run the example you will notice that only default value is shown in targets, eventhough we select another color then target's value can not change.

OneTime Binding mode
OneTime Binding Mode
           We can use this to show read only data in control as it was loaded. We can use this to compare the changes occurred in the UI with Default values. This is best when source Doesn't Implement INofityPropertyChanged Interface.


         This mode is reverse to OneWay Mode, When Mode is set to OneWayToSource then if target value is changed by user then that value is updated to source.  In our example change the Mode to OneWayToSource and run the example you will notice that target's can not have any values, Now write any color in TextBox, when focus leaves then source will be updated for default selectedindex not the current selected index. But here Changed Data in Source can not be updated in target.

OneWayToSource Binding Mode example
OneWayToSource Binding Mode

       We have to use this Binding Mode when user can able to change the source data and changed data can not be updated in Target UI.


       If you set Mode to TwoWay then Whenever Source Property Changed then Target will be udpated, similarly if target values changes, changes will be updated in source property. Set the Mode two way in our example run it, You will notice that whenever  ListBox selection changes it will be updated in TextBlock and TextBoxes. If we change the value in TextBox then Source is Updated and also target updated with these values.
TwoWay DataBinding Mode example
TwoWay Data Binding

   Use this binding when user can able to change data source and that changes will be updated to targets.

Default: In addition to these four Binding Modes Default Binding Mode exist. If you don't specify any binding Mode in Binding element then Default will take place. Defualt binding mode choose binding mode depending upon the target controls Dependency Property. For Example TextBox control Text property has binding mode as TwoWay in the same it will take for other controls.


      It is the property of Binding element, indicates or defines when should be the target value updated in the source.


        I hope you are clear about binding modes in wpf. So what do you thinking about binding modes now.

             Don't forget to Comment and Share for our friends if you like this post. Thank You.

Related Posts:

No comments:

Post a Comment

Please Give your Valuable Suggestions,Questions and Comments to improve my blog.