Binding Delay 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 can reverse 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 the control properties reverse. This synchronization between the source and the target happens immediately.

But what if you want to delay the synchronization? This is possible using the Delay property of the Binding object. The Delay property represents the amount of time, in milliseconds, to wait before updating the binding source after the value on the target changes.

The code snippet in Listing 1 creates a Stack Panel with a Slider and a TextBox control as child elements. The Value property of the Slider control is bound to the Text property of the TextBox.

<StackPanel Orientation="Vertical">

            <Slider Name="ValueSlider" Minimum="0" Maximum="100" VerticalAlignment="Top"

                 Margin="20" Height="25" Value="{Binding ElementName=ValueText, Delay=500, Path=Text, Mode=TwoWay}"></Slider>

            <TextBox Name="ValueText" Text="50"  Height="30" FontSize="20" Width="200"></TextBox>

</ StackPanel>


 

Listing 1

 

The output of Listing looks like Figure 1.

 

 
Figure 1
 

The value of the TextBox.Text will update when the user moves the slider with a delay of .5 seconds. If you remove the Delay and its value, the slider will have no delay.

Here is a list of recommended data binding articles:

Data Binding in WPF
In this article, I discuss how to use ADO.NET DataSet to get data from a database and bind to a WPF ListBox control using data binding process in WPF

Two-Way Databinding in WPF
This article kick starts your two way data-binding model in WPF

WPF Binding Cheat Sheet
This is an article on WPF Binding Cheat Sheet.

Binding Source Objects in WPF
This article will drive you through the various available bindings

Up Next
    Ebook Download
    View all
    Learn
    View all