【问题标题】:Get data from NewItemRow after row inserted in grid在网格中插入行后从新项目行获取数据
【发布时间】:2023-03-31 02:51:01
【问题描述】:

我正在使用 DevExpress XPF GridControl 的 NewItemRow 向我的数据库添加新行。如何从新行中获取用户输入的数据。我正在使用棱镜框架。这是我的xml

         <dxg:GridControl.View>
            <dxg:TableView AutoWidth="True" AllowEditing="True" NewItemRowPosition="Top">       
                <dxmvvm:Interaction.Behaviors>
                    <dxmvvm:EventToCommand EventName="RowUpdated" 
                                           Command="{Binding RowUpdateClickCommand}" CommandParameter="{Binding CurrentItem}"/>
                </dxmvvm:Interaction.Behaviors>
            </dxg:TableView>
        </dxg:GridControl.View>

【问题讨论】:

    标签: wpf datagrid devexpress devexpress-wpf newrow


    【解决方案1】:

    要获取有关更新行的信息,您可以将 EventArgs 传递给您的命令。要完成此任务,请将 EventToCommand.PassEventArgsToCommand 属性设置为 true:

    <dxmvvm:EventToCommand EventName="RowUpdated" PassEventArgsToCommand="True"
                            Command="{Binding RowUpdateClickCommand}"/>
    

    要确定用户修改了 NewItemRow,您可以将 RowEventArgs.RowHandle 与静态 GridControl.NewItemRowHandle 属性进行比较:

    public class MyViewModel {
        public MyViewModel() {
            RowUpdateClickCommand = new DelegateCommand<RowEventArgs>(RowUpdateClick);
        }
        public ICommand RowUpdateClickCommand {
            get;
            set;
        }
        public void RowUpdateClick(RowEventArgs e) {
            if (e.RowHandle == GridControl.NewItemRowHandle) {
                //e.Row -  new row is here
            }
        }
    }
    

    请注意,如果您不希望将事件参数传递到视图模型级别,可以使用 EventArgsConverter 进行转换

    【讨论】:

    • 如何将此事件传递给视图模型?你能分享一些例子吗
    • 我已更新我的答案以演示如何定义命令以使其接受 RowEventArgs。当您将 PassEventArgsToCommand 设置为 true 时,EventToCommand 将在引发事件时自动将事件参数传递给命令
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2018-03-03
    • 2019-11-05
    相关资源
    最近更新 更多