Thursday, January 27, 2011

Buffered Binding

WPF Bindings support three different update triggers:



  • PropertyChanged: Updates the binding source immediately whenever the binding target property changes.


  • LostFocus: Updates the binding source whenever the binding target element loses focus.


  • Explicit: Updates the binding source only when you call the UpdateSource method.


In my last application I found LostFocus and PropertyChanged to be inadequate for my requirements. PropertyChanged would trigger some costly validation operations on every key stroke (overkill), while PropertyChanged would require the user to "click out" of the input area to activate validation. On single input forms this made for a very confusing user interface.


The solution in WPF for this is to use the Explicit UpdateSourceTrigger and then make informed decisions about when to update the source binding. For my purposes, I found the best experience was achieved by triggering a SourceUpdate only when the user stops typing for some period of time (I picked 1.5 seconds). Writing this code for every input field did not appeal to me, so I wrote an attached behaviour to handle this. I also found the syntax of the attached behaviour ugly, so I wrote a markup extension to automate the creation of the attached behaviour. The final syntax would look like this:



<TextBox Text="{Util:BufferedBinding {Binding Name, ValidatesOnDataErrors=True}}"/>



I call this the BufferedBinding and the code is available below.


BufferedBinding.cs



Tuesday, January 25, 2011

First Post


Testing code formatting.



for(int i = 0; i< 10; i++)