Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGrid的绑定ObservableCollection被OnCollectionChanged覆盖_C#_Wpf_Datagrid_Observablecollection - Fatal编程技术网

C# DataGrid的绑定ObservableCollection被OnCollectionChanged覆盖

C# DataGrid的绑定ObservableCollection被OnCollectionChanged覆盖,c#,wpf,datagrid,observablecollection,C#,Wpf,Datagrid,Observablecollection,我已将WPF数据网格绑定到ObservableCollection。当用户单击datagrid时,键入文本并按enter键,他会添加新行,我需要获取这一行并执行一些代码 public class WatchListData<Quote>: ObservableCollection<Quote> { protected override void OnCollectionChanged(System.Collections.Specialized.Notif

我已将WPF数据网格绑定到ObservableCollection。当用户单击datagrid时,键入文本并按enter键,他会添加新行,我需要获取这一行并执行一些代码

    public class WatchListData<Quote>: ObservableCollection<Quote>
{
    protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        base.OnCollectionChanged(e);
        MessageBox.Show(e.Action.ToString());
    }
 }
公共类WatchListData:ObservableCollection
{
CollectionChanged上的受保护覆盖无效(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
基础。变更的集合(e);
Show(例如Action.ToString());
}
}

问题是,它在用户开始键入时触发,而不是在用户完成键入并按enter键时触发。所以我无法获取用户键入的内容。也只触发“.Add”操作类型,但我需要捕获用户更改现有行的时间。请指导我该怎么做。

您可以尝试在按下键时捕获事件并检查enter键。@sexta13编辑单元格只需单击其他单元格或窗口即可完成。我需要捕获Cells的任何更新,然后您可以有两个事件:OnKeyPressed和LostFocus@sexta13这将是低层次的方法,我不需要绑定和可观察的集合。我想用MVVM风格来做,而不是接触datagrid.ok。尽管如此,您仍然可以使用带有MVVM灯的eventToCommand(例如)。无论如何,当用户单击其他单元格或窗口时,可以更改OnCollection。但是对于enter键,我认为您必须使用类似OnKeyPressed的东西并检查enter键。这一切都可以在ViewModel中通过EventToCommand完成