WPF DataGrid 全行编辑
我有一个 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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不再使用 DataGrid 进行编辑。我使用 ListView 然后提供 GridView 作为 ListView.View。在 GridView 内部,您可以使用 CellTemplates 创建 GridViewColumns。每个 GridView 行的最后一列是一个用于删除该行的按钮。我不支持浏览和编辑模式,只支持编辑模式。该应用程序运行更加流畅,并且我没有遇到使用 DataGrid 带来的任何麻烦。
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.
全行编辑是默认功能。每次单元格编辑触发更新的唯一原因是您已经实现了 dg_cellEditEnding 方法。
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.