I have a DataGrid that contains information from a settings object. There is currently a two-way binding between the DataGrid and the settings. However, I want to put the Save button, which only links the changes made to the DataGrid to the object, if the user clicks the Save button. However, I'm not sure how to call UpdateSource () for my specific case with my DataGrid.
Here is my xaml.cs code:
public void LoadDataFields(Data d)
{
Grid1.ItemsSource = d.Fields;
}
private void SaveChanges(object sender, RoutedEventArgs e)
{
BindingExpression be = Grid1.GetBindingExpression(DataGrid.ItemsSourceProperty);
be.UpdateSource();
}
Here is my xaml code:
<DataGrid x:Name="Grid1"
IsReadOnly="False"
Height="360"
Margin="20,15,20,15"
VerticalAlignment="Top"
AutoGenerateColumns="False"
CanUserAddRows="False" SelectionUnit="Cell"
ItemsSource="{Binding data}"
>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Field">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=name, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Length of Field">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=length, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Is there an easy way to call UpdateSource () so that the binding only happens when the "Save" button is clicked? I assume that I just put the wrong property in the GetBindingExpression method.