WPF DataGrid - CellEditEnding 事件更新数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13516324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid - CellEditEnding event update data
提问by Aaginor
I am struggling with the DataGrid in WPF. I have a ObservableCollection bound to it. When the user enters the first cell, the other cells will update accordingly. To achieve that, I subscribed to the CellEditEnding event to force the update after the first cell has been changed.
我正在为 WPF 中的 DataGrid 苦苦挣扎。我有一个绑定到它的 ObservableCollection。当用户输入第一个单元格时,其他单元格将相应更新。为了实现这一点,我订阅了 CellEditEnding 事件以在第一个单元格更改后强制更新。
In this event, I also update other properties of MyClass like this:
在这种情况下,我还会像这样更新 MyClass 的其他属性:
private void DataGridTeilnehmer_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (!commiting)
{
commiting = true;
DataGridTeilnehmer.CommitEdit(DataGridEditingUnit.Row, false);
commiting = false;
if (e.Column.DisplayIndex == 0)
{
MyClass data = (e.Column.GetCellContent(e.Row) as ContentPresenter).Content as MyClass;
data.pass = "nothing";
}
}
The problem is, that the Grid doesn't update itself so "nothing" is not be showed, until I enter edit-mode of the cell that is bound to the property "pass" which contains "nothing". But I would like to show it immediately.
问题是,Grid 不会自行更新,因此不会显示“nothing”,直到我进入绑定到包含“nothing”的属性“pass”的单元格的编辑模式。但我想立即展示它。
Thanks in advance,
Frank
提前致谢,
弗兰克
PS: I have worked with many (Data)Grids in my life, but the WPF Grid is the worst I encountered so far.
PS:我一生中使用过许多(数据)网格,但 WPF 网格是我迄今为止遇到的最糟糕的。
回答by daniele3004
The correct way is as follows, currently use this way in my software
正确的方式如下,目前我的软件中使用这种方式
private void MyWPFFrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.Column.SortMemberPath.Equals("EndDate"))
{
if (((MyObject)e.Row.Item).EndDate.Equals(DateTime.MinValue))
{
((MyObject)e.Row.Item).Completed = 1;
((MyObject)e.Row.Item).CompletedDescription = "YES";
}
else
{
((MyObject)e.Row.Item).Completed = 0;
((MyObject)e.Row.Item).CompletedDescription = "NO";
}
this.MyWPFFrid.CurrentItem = ((MyObject)e.Row.Item);
if (!e.Row.IsEditing)
{
this.MyWPFFrid.Items.Refresh();
}
}
}

