Save the object directly after editing in the DataGrid
I am using DataGrid
to visualize my data to the user. After editing, the updated data should be stored directly in the database without using the "Save" button.
This is my solution so far, which works for all columns DataGrid
, but ComboBox
using EventTrigger
to InvokeCommandAction
:
<DataGrid ItemsSource="{Binding Path=Animals, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectionMode="Single" AutoGenerateColumns="False" CanUserSortColumns="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="RowEditEnding">
<i:InvokeCommandAction Command="{Binding SaveCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Header="EPC" Binding="{Binding Epc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Visual ID" Binding="{Binding VisualId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Geschlecht" Binding="{Binding Gender, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTemplateColumn Header="Bucht">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.Pens, RelativeSource={RelativeSource AncestorType={x:Type view:AdministrationView}}}" DisplayMemberPath="Name" SelectedItem="{Binding Pen, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding Pen.PenId}" SelectedValuePath="PenId"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
How can I call SaveCommand on my viewmodel after changing the ComboBox selection?
Or is there a simpler solution to achieve the desired behavior (automatically save to the data store after every edit)?
source to share
You get the desired behavior if you just add another one EventTrigger
that activates yours ICommand
like this:
<DataGridTemplateColumn Header="Bucht">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.Pens, RelativeSource={RelativeSource AncestorType={x:Type view:AdministrationView}}}" DisplayMemberPath="Name" SelectedItem="{Binding Pen, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding Pen.PenId}" SelectedValuePath="PenId">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding DataContext.SaveCommand, RelativeSource={RelativeSource AncestorType={x:Type view:AdministrationView}}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
source to share
In your ViewModel, am I assuming your selected item is defined this way?
object selectedPen;
public object SelectedPen
{
get
{
return selectedPen;
}
set
{
if (selectedPen != value)
{
selectedPen = value;
DoSave(); // insert saving logic here
OnPropertyChanged("SelectedPen");
}
}
}
You can trigger save when the value has changed in the installer.
source to share
The way to do it is to implement IEditableObject on your ViewModel and implement DataGrid.CellEditEnding
and DataGrid.CurrentCellChanged
. See this blog post from Colin Eberhardt for more details .
private DataRowView rowBeingEdited = null;
private void dataGrid_CellEditEnding(object sender,
DataGridCellEditEndingEventArgs e)
{
DataRowView rowView = e.Row.Item as DataRowView;
rowBeingEdited = rowView;
}
private void dataGrid_CurrentCellChanged(object sender, EventArgs e)
{
if (rowBeingEdited != null)
{
rowBeingEdited.EndEdit();
}
}
source to share