Search content within the blog

Monday, December 5, 2011

Updating Source in WPF Data Binding

In WPF data binding, when source property is updated from target property is called as updating source property and is defined using UpdateSourceTrigger property of Binding object. This property is used to define how you want to update source. There are two binding modes are available TwoWay and OneWayToSource which causes update to source property on change of target property.

You can specify mainly three different values to UpdateSourceTrigger property of Binding. If you don’t specify any value to UpdateSourceTrigger it uses default.


PropertyChanged – It updates source property as soon as target property changed.

<TextBox Name="CustomerName" Height="30" Margin="5"
Text="{Binding Source={StaticResource MyCustomerData},
Path=CustomerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Grid.Row="0" Grid.Column="1" />

LostFocus – When target object loses focus as soon as source property is updated.

<TextBox Name="CustomerName" Height="30" Margin="5"
Text="{Binding Source={StaticResource MyCustomerData},
Path=CustomerName, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>

Explicit – Source property is updated when you call explicitly UpdateSource method from code.

<TextBox Name="CustomerName" Height="30" Margin="5"
Text="{Binding Source={StaticResource MyCustomerData},
Path=CustomerName, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>

Code:


BindingExpression bindingExpression =
CustomerName.GetBindingExpression(TextBox.TextProperty);
bindingExpression.UpdateSource();

No comments:

Post a Comment