I have two-way binding between a DataGrid and an object. I want to only save changes made in the DataGrid to the object when the user clicks a save button. As a first step, I set UpdateSourceTrigger=Explicit. I would have expected no binding to occur when I set that property to explicit (since I have not called UpdateSource() yet), but contrary to my expectation, the changes are bound to the object when I close and restart the program.
Why are my changes still binding to my object's settings?
Here is my relevant DataGrid code from my xaml file:
<DataGrid x:Name="DataGrid1" IsReadOnly="False"
AutoGenerateColumns="False" CanUserAddRows="False" SelectionUnit="Cell"
ItemsSource="{Binding data}">
<DataGrid.DataContext>
<Binding Source="{StaticResource myData}" UpdateSourceTrigger="Explicit"/>
</DataGrid.DataContext>
<DataGrid.Columns>
<DataGridTextColumn Header="Field" Binding="{Binding Path=name, Mode=TwoWay,
UpdateSourceTrigger=Explicit}" Width="Auto"/>
<DataGridTextColumn Header="Length of Field" Binding="{Binding Path=length, Mode=TwoWay,
UpdateSourceTrigger=Explicit}" Width="Auto"/>
</DataGrid.Columns>
</DataGrid>
Try with a DataGridTemplateColumn instead of that TextColumn:
Data class is here :
And my test code:
It will get to the PropertyChanged events only at the beginning and after that, when modifying values from the GUI it won't trigger. So at the end, you will be able to save your modifications from code behind.