WPF DataGrid Full Row Editing

允我心安 提交于 2021-02-19 08:02:13

问题


I have a WPF datagrid which works for what I want, but there are two cells in each row which can be edited. Is it possible to place both of these rows into edit mode when the row is edited, then fire the update when the row edit ends/the row loses focus? Currently, after each cell is edit, RowEditEnding fires and the user must wait for the UI to redraw after the commit. The code I'm using is:

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

回答1:


I quit using DataGrid for editing. I use the ListView then provide then provide a GridView as the ListView.View. Inside the GridView you can create GridViewColumns with CellTemplates. The last column of each GridView row is a button to delete that row. Instead of supporting a browse and edit mode, I just support an edit mode. The application moves more fluidly and I don't have any of the headaches that comes along with working with the DataGrid.




回答2:


Full row editing is the default functionality. The only reason the update is firing per cell edit is that you have implemented the dg_cellEditEnding method.



来源:https://stackoverflow.com/questions/5975398/wpf-datagrid-full-row-editing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!