I often get questions about WPF binding behaving differently than people expect in their usage case so here is post to help out with that.

When you bind to properties in WPF with for example: Value=”{Binding Path=MyProperty}” the binding update behavior, i.e the trigger for value update, is controlled by the defaults set on the Dependency Property. For example on our WPF Input controls here is how the Value property is declared:

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(“Value”, typeof(int?), typeof(IntegerInput), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, null, true, UpdateSourceTrigger.LostFocus));

Notice the UpdateSourceTrigger.LostFocus that specifies the update is performed when the input control has lost focus. This is also default behavior for system text box Text property.

To change that you simply specify the UpdateSourceTrigger in your binding declaration as follows:

Value="{Binding Path=MyProperty, UpdateSourceTrigger=PropertyChanged}"

This will update the bound property whenever property value changes.

The UpdateSourceTrigger enumeration has following values you can use:

  • Default – The default UpdateSourceTrigger value of the binding target property. The default value for most dependency properties is PropertyChanged, while the Text property has a default value of LostFocus.
  • 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.

Hope this helps.

Professional looking applications made easy with DotNetBar for WinForms, Silverlight and WPF User Interface components. Click here to find out more.