简体   繁体   English

WPF DataGrid SelectedItem绑定在项目更改后停止工作

[英]WPF DataGrid SelectedItem binding stops working after item change

My problem/situation is very similar to Wpf DataGrid SelectedItem loses binding after cell edit but I'm not using any "custom" WPF framework. 我的问题/情况非常类似于Wpf DataGrid SelectedItem在单元格编辑后失去绑定但我没有使用任何“自定义”WPF框架。 I have a model that implements INotifyPropertyChanged and IEditableObject , and a grid bound to an ObservableCollection<T> . 我有一个实现INotifyPropertyChangedIEditableObject的模型,以及一个绑定到ObservableCollection<T>的网格。 The grid's SelectedItem property is bound to a property on the VM. 网格的SelectedItem属性绑定到VM上的属性。

With a break point, I can see my ViewModel.SelectedItem property change as I select different rows in the grid. 有了断点,我可以看到我的ViewModel.SelectedItem属性发生变化,因为我选择了网格中的不同行。 The moment I change a value on a row, however, the ViewModel.SelectedItem property is no longer set as I change focus on the rows. 但是,当我更改行上的值时,不再设置ViewModel.SelectedItem属性,因为我将更改焦点放在行上。 The solution identified in the above link does not work since I'm not using a custom WPF framework, just naked WPF. 上面链接中标识的解决方案不起作用,因为我没有使用自定义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;
            }
        }
    }
}

Apparently the SelectedItem dependency property on DataGrid is broken and not being used correctly. 显然,DataGrid上的SelectedItem依赖项属性已损坏且未正确使用。 After some debugging using OnPropertyChanged, I found that the grid is actually setting a "CurrentItem" property reliably. 在使用OnPropertyChanged进行一些调试之后,我发现网格实际上是可靠地设置了“CurrentItem”属性。 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. 我改为使用CurrentItem,一切似乎都正常工作......用户的“选定行”正在被发送到VM而没有发生任何事故。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM