具有 TrulyObservableCollection 的 WPF Datagrid 失去了对编辑的关注

Vin*_*nay 2 c# wpf observablecollection mvvm wpfdatagrid

您好,我有一个与 绑定的 WPF 数据网格TrulyObservableCollection,每当我尝试通过文本输入编辑单元格时,数据网格单元格在输入每个键后都会失去焦点,

public sealed class TrulyObservableCollection<T> : ObservableCollection<T>
    where T : INotifyPropertyChanged
{
    public TrulyObservableCollection()
    {
        CollectionChanged += FullObservableCollectionCollectionChanged;
    }

    public TrulyObservableCollection(IEnumerable<T> pItems)
        : this()
    {
        foreach (var item in pItems) {
            this.Add(item);
        }
    }

    private void FullObservableCollectionCollectionChanged(object sender,
        NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null) {
            foreach (Object item in e.NewItems) {
                ((INotifyPropertyChanged)item).PropertyChanged += ItemPropertyChanged;
            }
        }
        if (e.OldItems != null) {
            foreach (Object item in e.OldItems) {
                ((INotifyPropertyChanged)item).PropertyChanged -= ItemPropertyChanged;
            }
        }
    }

    private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        try {
            var a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
            OnCollectionChanged(a);
        }
        catch {
            // ignored
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

下面是我的 ViewModel,它绑定到 Datagrid,

public TrulyObservableCollection<SubLotModel> SubLotCollection
{
    get { return _subLotCollection; }
    set
    {
        _subLotCollection = value;
        NotifyOfPropertyChange(() => SubLotCollection);
        SerialNumberAdded = _subLotCollection.Count;
    }
}
Run Code Online (Sandbox Code Playgroud)

在数据网格中,我有一个文本框列,它绑定到 SublotCollection 中的 Quantity 属性,

<DataGridTemplateColumn Header="Quantity">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Quantity, ValidatesOnDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>

        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Run Code Online (Sandbox Code Playgroud)

一旦我在文本框列中输入有效键,单元格就会失去焦点。

Grx*_*x70 5

诊断

正如我在评论中提到的,您的编辑失去焦点的原因是这一系列事件:

  • 您输入一个字母,新文本会被推送到视图模型,因为UpdateSourceTrigger=PropertyChanged
  • Quantity视图模型上的属性已更新并引发PropertyChanged事件
  • TrulyObservableCollection处理该PropertyChanged事件并因此引发CollectionChanged事件,并将操作设置为NotifyCollectionChangedAction.Reset
  • DataGrid单元格旨在在这种情况下进行自我重建,因此每个单元格都是从头开始构建的(重新模板化),最终会出现一个全新的单元格,TextBox其中曾经是您在字母中键入的单元格

焦点不会返回到 new ,TextBox因为 theDataGrid并没有真正意识到它的存在(因为它是在 a 中定义的DataTemplate)。我认为即使您使用其中任何一个,焦点也不会恢复DataGridTextColumn(但您可以检查)。

所以这里的核心问题是NotifyCollectionChangedAction.Reset. 当仅更改一件物品的一项属性时,声称该系列发生了巨大变化似乎完全是矫枉过正。

解决方案

更合理的选择是提出CollectionChanged包含NotifyCollectionChangedAction.Replace哪个项目被“替换”的信息。除了一些改进之外,这里还有一个集合实现,其行为应该像您期望的那样(当我测试它时):

public class TrulyObservableCollection<T> : ObservableCollection<T> 
    where T : INotifyPropertyChanged
{
    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        base.OnCollectionChanged(e);
        if (e.NewItems != null)
        {
            foreach (INotifyPropertyChanged inpc in e.NewItems)
                    inpc.PropertyChanged += Item_PropertyChanged;
        }
        if (e.OldItems != null)
        {
            foreach (INotifyPropertyChanged inpc in e.OldItems)
                    inpc.PropertyChanged -= Item_PropertyChanged;
        }
    }

    private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var index = IndexOf((T)sender);
        var args = new NotifyCollectionChangedEventArgs(
            action: NotifyCollectionChangedAction.Replace, 
            newItem: sender, 
            oldItem: sender, 
            index: index);
        //no need to call the override since we've already
        //subscribed to that item's PropertyChanged event
        base.OnCollectionChanged(args); 
    }
}
Run Code Online (Sandbox Code Playgroud)