Different Ways of Binding in WPF

It requires knowledge of Binding concept in WPF.

Following are the different ways of Binding in WPF:

  1. Datacontext binding:

    Binds property to Datacontext object.

    E.g.: Consider following Xaml snppet.
    1. <TextBox Text="{Binding}" />  
    Binds Text to Datacontext object.

  2. Property Binding with change notification from source:

    Consider following Xaml snppet.
    1. <TextBox Text="{Binding Path=EmpName}" />  
    Corresponding View Model looks as follows:
    1. public class ViewModel: System.ComponentModel.INotifyPropertyChanged  
    2. {  
    3.     private string _EmpName = "Hi to Binding";  
    4.     private string EmpName  
    5.     {  
    6.         get  
    7.         {  
    8.             return _EmpName;  
    9.         }  
    10.         set  
    11.         {  
    12.             _EmpName = value;  
    13.             OnPropertyChanged("EmpName");  
    14.         }  
    15.     }  
    16.     public event System.ComponentModel.PropertyChangedEventHandler OnPropertyChangedEvent;  
    17.     protected virtual void OnPropertyChanged(string propertyName)  
    18.     {  
    19.         if (this.OnPropertyChangedEvent != null)  
    20.         {  
    21.             this.OnPropertyChangedEvent(thisnew System.ComponentModel.PropertyChangedEventArgs(propertyName));  
    22.         }  
    23.     }  
    24. }  
  3. Property Binding with change notification from client:
    1. <TextBox Text="{Binding Path=EmpName,   
    2. UpdateSourceTrigger=PropertyChanged}" />  
    UpdateSourceTrigger notifies when notification should be sent to source about change. Default UpdateSourceTrigger is LostFocus.

    If UpdateSourceTrigger is set to LostFocus then it mean when Text value of textbox changes, it notifies to source on Lostfocus e.g. if User types in textbox then it will not change EmpName. It changes its value after losing focus.

    Using UpdateSourceTrigger=PropertyChnaged, changes EmpName as soon as you type value in textbox.

  4. FallBack value:
    1. <TextBox Text="{Binding Path=EmpName,   
    2. FallbackValue='Employee Name'}" />  
    FallbackValue is displayed if element is unable to resolve the binding i.e. if the property does not exists in the ViewModel.

  5. Element Binding:

    You can bind UI element to other UI element.
    1. <TextBox Name="txt1" Text="{Binding Path=EmpName}" />  
    2.    <TextBox Name="txt2" Text="{Binding Path= Text,   
    3. ElementName=txt1}" />  
  6. Binding with Converter:

    Converter class as follows:
    1. public class BooleanToTextConverter: System.Windows.Data.IValueConverter  
    2. {  
    3.     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
    4.     {  
    5.         //do something and return value to UI element  
    6.     }  
    7.     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
    8.     {  
    9.         //do something and return value to ViewModel  
    10.     }  
    11. }  
    Add Namespace of BooleanToTextConverter class in windows tag as:
    1. <Window …..  
    2. Xmlns:common="clr-namespace:Common.Library;assembly:common.Library">  
    Create instance of converter class in XAML:
    1. <Window.Resources>  
    2.    <common:BooleanToTextConverter x:key="TextConverter"/>  
    3. </Window.Resources>  
    Bind this instance as:
    1. <TextBox Name="txt1" Text="{Binding Path=EmpName,   
    2. Converter={StaticResource TextConverter }}" />