c# - 在生成的 EF 类中添加 INotifyPropertyChange

标签 c# wpf entity-framework mvvm

所以,我的类是由 EF 自动生成的:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Testje.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Ploeg
    {
        public Ploeg()
        {
        }

        public int Id { get; set; }
        public string Naam { get; set; }
        public string Icon { get; set; }
    }
}

当其中一个属性发生变化时,我想做一个 notifypropertychange。如果不编辑这个生成的类,这是否可能以某种方式实现?

最佳答案

这是可能的,但我强烈建议不要这样做! 在您的模型中添加 INotifyPropertyChanged 将导致 关注点分离不佳。

使用事件来突出显示模型已更改。 在这里看看它是如何工作的:MSDN on standard event pattern

或者更好:MSDN cites Albahari Bros on the event pattern

在您的 View 模型中,实现 INotifyProperty 已更改。 让您的 ViewModel 监听模型中的事件,调整数据 并通过 INotifyPropertyChanged 将其推送到您的 View

关于c# - 在生成的 EF 类中添加 INotifyPropertyChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13443162/

相关文章:

c# - 在 Entity Framework Code First 中忽略基类型是否也会忽略子类?

c# - Byte Vigenere Cipher,解密错误

c# - 在 C# 中,为什么 string.Empty 是一个字段而不是常量?

c# - 第一个单元测试(VS2010 C#)

c# - 如何在 blob 容器中使用 azure 搜索搜索最新文件中的匹配数据 C#

c# - 如何使用 InvokeCommandAction 调用我的方法并传入参数?

c# - 将 ASP.NET 4 Web 应用程序部署到 IIS6 服务器后,工作进程回收导致其大部分时间停止工作

c# - Entity Framework - 如何将连接字符串更改为相对的?

c# - 如何将UpdateSourceTrigger的值交给UserControl或者运行时更新?

c# - 将一个类中的值绑定(bind)到另一类中的另一个值