【问题标题】:How to get DataGrid row data after user has finished editing row?用户完成编辑行后如何获取 DataGrid 行数据?
【发布时间】:2011-06-08 03:00:53
【问题描述】:

我想在用户在数据网格中输入一行后立即验证用户输入的内容。

我应该查看什么事件,以及如何检索行数据?或者更好的是,它绑定到的对象?

【问题讨论】:

    标签: c# wpf wpf-controls wpfdatagrid


    【解决方案1】:

    如果您遇到问题,我已成功使用:

    DataGridCellInfo selected = YourDataGrid.SelectedCells[0];
    YourObject selectedRow = selected.Item as YourObject; 
    

    【讨论】:

      【解决方案2】:

      使用RowEditEnding 事件。

      private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
      {
        YourObject obj = e.Row.Item as YourObject;
        if (obj != null)
        {
           //see obj properties
        }
      }
      

      【讨论】:

      • 我看到了,我看到了e 上的Row 属性,但我仍然不确定如何检索输入的数据?
      • 它有DataGridRowEditEndingEventArgs,它有行数据对象。如果您看到奇怪的行为,请详细说明。 (见我的编辑)
      • 啊...e.Row.Item 是存储数据的地方...这就是我想知道的!
      【解决方案3】:
      1. 事件行编辑结束
      2. 数据应该在 e.Row.DataContext/e.Row.Item 中

      【讨论】:

      • DataContext和Item有什么区别?
      • 如果项目是从 DataGrid 上的 ItemsSource 绑定生成的,则将设置 DataContext,在这种情况下,Item 和 DataContext 将是相同的。 DataContext 用于允许相对于给定上下文的绑定路径。
      猜你喜欢
      • 1970-01-01
      • 2021-12-18
      • 2011-11-10
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 1970-01-01
      • 2013-05-04
      相关资源
      最近更新 更多