WPF DataGrid SelectedItem 绑定在项目更改后停止工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17309915/
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 SelectedItem binding stops working after item change
提问by Martin Bliss
My problem/situation is very similar to Wpf DataGrid SelectedItem loses binding after cell editbut I'm not using any "custom" WPF framework. I have a model that implements INotifyPropertyChangedand IEditableObject, and a grid bound to an ObservableCollection<T>. The grid's SelectedItemproperty is bound to a property on the VM.
我的问题/情况与Wpf DataGrid SelectedItem 在单元格编辑后丢失绑定非常相似,但我没有使用任何“自定义”WPF 框架。我有一个实现INotifyPropertyChangedand的模型IEditableObject,以及一个绑定到ObservableCollection<T>. 网格的SelectedItem属性绑定到 VM 上的属性。
With a break point, I can see my ViewModel.SelectedItemproperty change as I select different rows in the grid. The moment I change a value on a row, however, the ViewModel.SelectedItemproperty is no longer set as I change focus on the rows. The solution identified in the above link does not work since I'm not using a custom WPF framework, just naked WPF.
通过断点,ViewModel.SelectedItem当我在网格中选择不同的行时,我可以看到我的属性发生了变化。但是,当我更改行上的值时,ViewModel.SelectedItem当我将焦点更改为行时不再设置该属性。上述链接中确定的解决方案不起作用,因为我没有使用自定义 WPF 框架,只是使用裸 WPF。
Any ideas?
有任何想法吗?
// View model area
public IPurchaseorderItem SelectedItem
{
get
{
return _selectedItem;
}
set
{
if (_selectedItem != value)
{
_selectedItem = value;
SelectItemCommand.NotifyCanExecuteChanged();
RemoveItemCommand.NotifyCanExecuteChanged();
}
}
}
// XAML SelectedItem binding
<views:NoBindingGroupDataGrid SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
// Special Grid to clear binding groups (read on a similarly themed SO question/answer)
internal sealed class NoBindingGroupDataGrid : DataGrid
{
private bool _editing = false;
protected override System.Windows.Size MeasureOverride(System.Windows.Size availableSize)
{
var desiredSize = base.MeasureOverride(availableSize);
ClearBindingGroup();
return desiredSize;
}
protected override void OnCellEditEnding(DataGridCellEditEndingEventArgs e)
{
base.OnCellEditEnding(e);
if (!_editing)
{
_editing = true;
CommitEdit(DataGridEditingUnit.Row, true);
_editing = false;
}
}
private void ClearBindingGroup()
{
ItemBindingGroup = null;
foreach (var item in Items)
{
var row = ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement;
if (row != null)
{
row.BindingGroup = null;
}
}
}
}
回答by Martin Bliss
Apparently the SelectedItem dependency property on DataGrid is broken and not being used correctly. After some debugging using OnPropertyChanged, I found that the grid is actually setting a "CurrentItem" property reliably. I changed to use CurrentItem instead and everything appears to work correctly... the user's "selected row" is being sent to the VM without incident.
显然,DataGrid 上的 SelectedItem 依赖项属性已损坏且未正确使用。在使用 OnPropertyChanged 进行一些调试后,我发现网格实际上可靠地设置了“CurrentItem”属性。我改为使用 CurrentItem 并且一切似乎都正常工作......用户的“选定行”正在毫无意外地发送到 VM。

