Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

WPF 4.5: Adding a delay to your binding updates to reduce noise

Pete Brown - 02 December 2011

A couple weekends back, I was giving a talk on the .NET Gadgeteer, and spoke about the support for Potentiometers. Unlike some of the other modules, a potentiometer (like the volume knob on a stereo) varies so much in use that you wouldn't want to raise an event for every little change. The solution in electronics is typically to poll the pot to find its current value.

Similar situations exist in XAML binding. Consider a scrollbar or slider. You may not want to send update notifications to the binding system for every micro movement of the slider as the user is sliding it from one side of the screen to the other. Instead, you may want to update only after a delay.

<Window x:Class="WpfBindingDelay.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Slider x:Name="ValueSlider"
Minimum="0" Maximum="100"
Margin="20" Height="25"
Value="{Binding ElementName=ValueText, Delay=500, Path=Text, Mode=TwoWay}" />
<TextBox x:Name="ValueText" Text="50"
Width="100" Height="50" FontSize="20"
HorizontalAlignment="Center" />

</StackPanel>

</Grid>
</Window>

The delay time is specified in milliseconds. In this case, I've specified 500ms (1/2 second) after the last update, to make the delay really obvious. This isn't a polling interval; the timer kicks in when you stop making changes. Normally you wouldn't use a value as large as 500, of course :)

Where you put the delay also has an impact. For example, if you bound the two controls together inside the TextBox's Text property and put the delay there, it wouldn't affect slider changes, only changes to the value typed into the TextBox.

Another useful place for this is when binding to data in a TextBox where you want to catch incremental changes, but the cost (for example, a search) is somewhat expensive. In that case, you can wait for a slight pause, maybe 100ms between keystrokes, and perform your search then.

       
posted by Pete Brown on Friday, December 2, 2011
filed under:        

5 comments for “WPF 4.5: Adding a delay to your binding updates to reduce noise”

  1. Rohit Sharmasays:
    Or alternatively you could use Reactive Extension and apply Throttle, that is what we do for purposes such as keystroke based events, where you do not want to kick off search/calculation on every key stroke

    But nice to have it implemented on the UI, I am going to link to this article. Thanks for sharing.
  2. Josh Einsteinsays:
    That is a great addition. I would still love to see a feature of the Binding markup extension that allows you to specify a refresh interval. That way you could support binding to properties that cannot easily implement INotifyPropertyChanged. A canonical example would be a TotalPrice property of an Order object that is based upon many sub-objects and their properties.
  3. Milansays:
    Sure it's easy solvable pattern, but when it's built-in like this then we're talking about icing on the cake here.

    Adding similar niche features would make this UI framework top notch.

    I just hope it won't be killed after v4.5 or at least that all its good things will translate into WinRT XAML flavor.

    - Milan

Comment on this Post

Remember me