C# WPF EF6数据网格单元不';除非用户编辑单元格,否则以编程方式更改时不会更新

C# WPF EF6数据网格单元不';除非用户编辑单元格,否则以编程方式更改时不会更新,c#,wpf,entity-framework,datagrid,C#,Wpf,Entity Framework,Datagrid,我有一个绑定到EF6 Dbcontext的WPF数据网格。我有很多功能,如手动编辑等 问题 编辑绑定到栅格的EF对象不会在栅格上更新。但是,如果在这些背景编辑之后手动编辑单元格,它会立即显示正确的值 场景/设置 查看涉及的资源 请注意,关联设备是设备对象内部的可观察集合 WPF数据网格(仅示例部分) 背景变化 组合框“cbxAssDevCertComboBox”selectionchanged事件随后更新其他单元格源对象的所有值。这是不刷新的更新 尝试/考虑过的主要选项 我还没有完全了解

我有一个绑定到EF6 Dbcontext的WPF数据网格。我有很多功能,如手动编辑等

问题 编辑绑定到栅格的EF对象不会在栅格上更新。但是,如果在这些背景编辑之后手动编辑单元格,它会立即显示正确的值

场景/设置

查看涉及的资源


请注意,关联设备是设备对象内部的可观察集合

WPF数据网格(仅示例部分)


背景变化 组合框“cbxAssDevCertComboBox”selectionchanged事件随后更新其他单元格源对象的所有值。这是不刷新的更新

尝试/考虑过的主要选项

  • 我还没有完全了解MVVM,我也没有跟上它。我现在太深了,无法改变这个项目。我只需要更新f'n的东西

  • 我不能把每个EF对象都包装在一个可观察的集合中。。。没有其他东西需要它,为什么?CollectionViewSource不就是这么做的吗

  • 更新Datagrids ItemSource bindingexpression没有帮助

  • 使用更新

  • 如何才能像在程序编辑后手动编辑单元格一样更新单元格?


    我现在已经浪费了好几天的时间试图解决这个问题;任何帮助都将不胜感激。

    一个
    可观察的集合
    非常好,但它只在添加或删除项目时通知用户界面。当项目被修改时(即项目属性被更改时),它不会通知UI

    您需要在模型上实现INotifyPropertyChanged接口并启动它

    下面是实现接口的示例模型类:

    public class ModelClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    
        protected virtual void OnPropertyChanged<T>(Expression<Func<T>> selectorExpression)
        {
            if (selectorExpression == null)
                throw new ArgumentNullException("selectorExpression");
            MemberExpression body = selectorExpression.Body as MemberExpression;
            if (body == null)
                throw new ArgumentException("The body must be a member expression");
            OnPropertyChanged(body.Member.Name);
        }
    
        string _myValue;
        public string MyValue
        {
            get
            {
                return _myValue;
            }
            set
            {
                _myValue = value;
                OnPropertyChanged(() => MyValue);
            }
        }
    }
    
    公共类模型类:INotifyPropertyChanged
    {
    公共事件属性更改事件处理程序属性更改;
    受保护的虚拟void OnPropertyChanged(字符串属性)
    {
    if(PropertyChanged!=null)
    PropertyChanged(此,新PropertyChangedEventArgs(property));
    }
    受保护的虚拟void OnPropertyChanged(表达式selectorExpression)
    {
    如果(selectorExpression==null)
    抛出新ArgumentNullException(“selectorExpression”);
    MemberExpression body=selectoreexpression.body作为MemberExpression;
    if(body==null)
    抛出新ArgumentException(“主体必须是成员表达式”);
    OnPropertyChanged(body.Member.Name);
    }
    字符串_myValue;
    公共字符串MyValue
    {
    得到
    {
    返回_myValue;
    }
    设置
    {
    _我的价值=价值;
    OnPropertyChanged(()=>MyValue);
    }
    }
    }
    
    俏皮的lambda把戏取材于


    当然,您可以从代码的其他部分手动调用
    OnPropertyChanged
    (根据需要调整访问器或编写额外的公共方法)。调用
    OnPropertyChanged
    应该强制UI为显示的属性调用getter。

    一个
    可观察集合
    很好,但它只在添加或删除项时通知UI。当项目被修改时(即项目属性被更改时),它不会通知UI

    您需要在模型上实现INotifyPropertyChanged接口并启动它

    下面是实现接口的示例模型类:

    public class ModelClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    
        protected virtual void OnPropertyChanged<T>(Expression<Func<T>> selectorExpression)
        {
            if (selectorExpression == null)
                throw new ArgumentNullException("selectorExpression");
            MemberExpression body = selectorExpression.Body as MemberExpression;
            if (body == null)
                throw new ArgumentException("The body must be a member expression");
            OnPropertyChanged(body.Member.Name);
        }
    
        string _myValue;
        public string MyValue
        {
            get
            {
                return _myValue;
            }
            set
            {
                _myValue = value;
                OnPropertyChanged(() => MyValue);
            }
        }
    }
    
    公共类模型类:INotifyPropertyChanged
    {
    公共事件属性更改事件处理程序属性更改;
    受保护的虚拟void OnPropertyChanged(字符串属性)
    {
    if(PropertyChanged!=null)
    PropertyChanged(此,新PropertyChangedEventArgs(property));
    }
    受保护的虚拟void OnPropertyChanged(表达式selectorExpression)
    {
    如果(selectorExpression==null)
    抛出新ArgumentNullException(“selectorExpression”);
    MemberExpression body=selectoreexpression.body作为MemberExpression;
    if(body==null)
    抛出新ArgumentException(“主体必须是成员表达式”);
    OnPropertyChanged(body.Member.Name);
    }
    字符串_myValue;
    公共字符串MyValue
    {
    得到
    {
    返回_myValue;
    }
    设置
    {
    _我的价值=价值;
    OnPropertyChanged(()=>MyValue);
    }
    }
    }
    
    俏皮的lambda把戏取材于


    当然,您可以从代码的其他部分手动调用
    OnPropertyChanged
    (根据需要调整访问器或编写额外的公共方法)。调用OnPropertyChanged应强制UI为显示的属性调用getter。

    已解决其他问题
    public class ModelClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    
        protected virtual void OnPropertyChanged<T>(Expression<Func<T>> selectorExpression)
        {
            if (selectorExpression == null)
                throw new ArgumentNullException("selectorExpression");
            MemberExpression body = selectorExpression.Body as MemberExpression;
            if (body == null)
                throw new ArgumentException("The body must be a member expression");
            OnPropertyChanged(body.Member.Name);
        }
    
        string _myValue;
        public string MyValue
        {
            get
            {
                return _myValue;
            }
            set
            {
                _myValue = value;
                OnPropertyChanged(() => MyValue);
            }
        }
    }