在DataGrid中编辑后直接保存实体 [英] Save entity directly after edit in DataGrid

查看:42
本文介绍了在DataGrid中编辑后直接保存实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 DataGrid 将数据可视化给用户.编辑后,更新的数据应直接存储到数据库中,而无需使用任何保存"按钮.

I use a DataGrid to visualize my data to the user. After an edit, the updated data should be stored directly to the database, without using any 'Save' button.

到目前为止,这是我的解决方案,通过将 EventTrigger 用作 InvokeCommandAction :

This is my solution so far, which works for all DataGrid columns but the ComboBox by using an 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>

更改组合框选择后如何在视图模型中调用SaveCommand?

或者是否有更简单的解决方案可完全实现我想要的行为(每次编辑后自动保存到数据存储区)?

Or is there an easier solution altogether to achieve my desired behaviour (auto save to data store after each edit)?

推荐答案

如果您仅添加另一个激活 ICommand EventTrigger ,您将获得所需的行为,如下所示:

You get the desired behaviour if you just add another EventTrigger that activates your ICommand like that:

<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>

这篇关于在DataGrid中编辑后直接保存实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆