Binding To Static Property in WPF 4.5

The Binding object in WPF is responsible for data binding with user interface elements and a data source. The Binding object is placed between a source and a target. The source can be a data source and the target can be a user control. We can also bind two object properties treating one of them as a source and one as a target.
In two-way binding mode, the synchronization happens between two object properties and each source and target reverses roles. For example, the TextBox.Text can be bound to the TextBlock.Text property. Initially, the TextBox.Text acts as a source and the TextBlock.Text acts as a target when a user updates the TextBox.Text. But when a user updates the TextBlock.Text, the value of the TextBox.Text is updated automatically and in this case the role of control properties reverses. This synchronization between the source and the target happens immediately.
In our applications, we often use static properties. Prior to WPF 4.5, the Binding object did not support binding with static properties. WPF 4.5 now supports binding with static properties.
Let's see how it is done with an example. In our example, we will bind a TextBox.Text property to the ApplicationTitle property of an application that is a static property.
Create a WPF application and name it Wpf_Static_Binding.
To access an object in XAML, we must declare the namespace in XAML. Declare the following code in your XAML code with other declarations.
  1. xmlns:local="clr-namespace:Wpf_Static_Binding" 
Now in XAML, we will add a replace Grid with a StackPanel and add a TextBox and a TextBlock control to the Window. We also bind the Text property using the Binding object as you can see in the following code.
  1. <StackPanel Orientation="Vertical">  
  2. <TextBox x:Name="TxtApplicationTitle"  
  3. Text="{Binding Path=(local:AppSettingApproach1.ApplicationTitle), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />  
  4. <TextBlock x:Name="TxtBlkApplicationTitle"  
  5. Text="{Binding Path=(local:AppSettingApproach1.ApplicationTitle)}" />  
  6. </StackPanel> 
Now, let's update the value of the static property, ApplicationTitle in the code.
  1. public Approch1Window()  
  2. {  
  3. InitializeComponent();  
  4. AppSettingApproach1.ApplicationTitle = @"Hello WPF, this my first Approach of static binding.";  
  5. }  
  8. public static class AppSettingApproach1  
  9. {  
  10. public static event EventHandler ApplicationTitleChanged;  
  12. private static string _applicationTitle;  
  13. public static string ApplicationTitle  
  14. {  
  15.  get { return _applicationTitle; }  
  16.  set  
  17.  {  
  18.   if (value != _applicationTitle)  
  19.   {  
  20.     _applicationTitle = value;  
  22.     if (ApplicationTitleChanged != null)  
  23.     ApplicationTitleChanged(null, EventArgs.Empty);  
  24.    }  
  25.   }  
  26.  }  


Nipun Tomar, Nitin Kumar for helping with the code.


In this small article, we discussed the static property binding feature added to WPF 4.5.