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.
- xmlns:local="clr-namespace:Wpf_Static_Binding"
- <StackPanel Orientation="Vertical">
- <TextBox x:Name="TxtApplicationTitle"
- Text="{Binding Path=(local:AppSettingApproach1.ApplicationTitle), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
- <TextBlock x:Name="TxtBlkApplicationTitle"
- Text="{Binding Path=(local:AppSettingApproach1.ApplicationTitle)}" />
- </StackPanel>
- public Approch1Window()
- {
- InitializeComponent();
- AppSettingApproach1.ApplicationTitle = @"Hello WPF, this my first Approach of static binding.";
- }
- public static class AppSettingApproach1
- {
- public static event EventHandler ApplicationTitleChanged;
- private static string _applicationTitle;
- public static string ApplicationTitle
- {
- get { return _applicationTitle; }
- set
- {
- if (value != _applicationTitle)
- {
- _applicationTitle = value;
- if (ApplicationTitleChanged != null)
- ApplicationTitleChanged(null, EventArgs.Empty);
- }
- }
- }
- }
Nipun Tomar, Nitin Kumar for helping with the code.
Summary
In this small article, we discussed the static property binding feature added to WPF 4.5.

Knarf VandPosted Sep 20, 2018, 5:31 AM
Would it be possible to have a second property and then bind to a new Textbox ?
Knarf VandPosted Sep 20, 2018, 5:30 AM
Hello Mahesh,