【问题标题】:WPF DataGrid Full Row EditingWPF DataGrid 全行编辑
【发布时间】:2011-08-23 22:35:38
【问题描述】:

我有一个 WPF 数据网格,可以满足我的需求,但每行中有两个单元格可以编辑。是否可以在编辑行时将这两行都置于编辑模式,然后在行编辑结束/行失去焦点时触发更新?目前,在编辑每个单元格后,RowEditEnding 会触发,用户必须等待 UI 在提交后重绘。我使用的代码是:

 private bool isManualEditCommit;
 private void dg_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            if(e.EditAction!= DataGridEditAction.Commit)
                return;
            var newProd = dgLists.SelectedItem as IProduct;
            if(newProd==null)
                return;
                    worker = new BackgroundWorker();
                    worker.DoWork += (s, dwe) =>
                    {
            ... commit update
                    };
                    worker.RunWorkerCompleted += (s, rwe) =>
                    {
                        ... refresh grid
                    };
                    worker.RunWorkerAsync();
        }
    /// <summary>
    /// Commits edits when a cell edit ends.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.Controls.DataGridCellEditEndingEventArgs"/> instance containing the event data.</param>
    private void dg_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
        if (e.EditAction == DataGridEditAction.Commit)
        {
            if (!isManualEditCommit)
            {
                isManualEditCommit = true;
                DataGrid grid = (DataGrid) sender;
                grid.CommitEdit(DataGridEditingUnit.Row, true);
                isManualEditCommit = false;
            }
        }

【问题讨论】:

    标签: wpf datagrid wpfdatagrid


    【解决方案1】:

    我退出使用 DataGrid 进行编辑。我使用 ListView 然后提供一个 GridView 作为 ListView.View。在 GridView 中,您可以使用 CellTemplates 创建 GridViewColumns。每个 GridView 行的最后一列是删除该行的按钮。我不支持浏览和编辑模式,只支持编辑模式。应用程序运行得更加流畅,我没有任何与使用 DataGrid 相关的头疼问题。

    【讨论】:

      【解决方案2】:

      整行编辑是默认功能。每次单元格编辑都会触发更新的唯一原因是您已经实现了 dg_cellEditEnding 方法。

      【讨论】:

      • 但并非所有单元格都进入编辑模式。每个单元格在获得焦点时都会显示它的 CellEditingTemplate,但该行中的其余单元格都有其标准的、不可编辑的控件。
      猜你喜欢
      • 2011-11-10
      • 2011-04-25
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 2013-11-03
      • 2013-07-30
      相关资源
      最近更新 更多