Pages

Tuesday, 28 September 2010

UpdateSourceTrigger in DataGrid

Inside a DataGrid, any control you add into a TemplateColumn which is bound TwoWay, used to use its default value of UpdateSourceTrigger, but the .NET 4 version of DataGrid overrides this by default to be Explicit.

For example:
<DataGrid>
<DataGrid.Columns>
<DataGrid.TemplateColumn>
<DataTemplate>
<TextBox Text={Binding MyProperty} />
</.....>

This used to work just fine.. When the value in TextBox changed, it would update MyProperty when the textbox lost focus (this is the default value for a textbox of UpdateSourceTrigger).
This no longer happens.
We now need to explicitly set the UpdateSourceTrigger for all UI controls inside a DataGrid Template column which should be updating their underlying property store.

Now becomes:
    <TextBox   Text={Binding MyProperty, UpdateSourceTrigger=LostFocus} />