C# 在生成的EF类中添加INotifyPropertyChange
因此,我的类是由EF自动生成的:C# 在生成的EF类中添加INotifyPropertyChange,c#,wpf,entity-framework,mvvm,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 appl
//------------------------------------------------------------------------------
// <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; }
}
}
//------------------------------------------------------------------------------
//
//此代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外行为。
//如果重新生成代码,将覆盖对此文件的手动更改。
//
//------------------------------------------------------------------------------
名称空间Testje.Models
{
使用制度;
使用System.Collections.Generic;
公共部分类Ploeg
{
公共图书馆
{
}
公共int Id{get;set;}
公共字符串Naam{get;set;}
公共字符串图标{get;set;}
}
}
当其中一个属性更改时,我希望执行notifypropertychange。如果不编辑这个生成的类,这是可能的吗?这是可能的,但我强烈建议不要这样做! 在模型中添加INotifyPropertyChanged将导致 关注点分离不好 使用事件突出显示模型已更改。 请查看此处以了解其工作原理: 或者更好: 在viewmodel中,实现INotifyProperty已更改。 让您的ViewModel监听模型中的事件,然后调整数据
并通过INotifyPropertyChanged将其推送到您的视图中是的,您可以使用名为PropertyChanged.Fody的NuGet包。安装包,然后创建另一个分部类,添加[ImplementPropertyChanged]属性,如下所示
using PropertyChanged;
[ImplementPropertyChanged]
public partial class Ploeg
{
}
就这样
有关更多信息,请参阅。在我的案例中,我添加了
InotifyProperty已更改
作为一种遗产,它似乎很有魅力。
使用EF生成的类(请求_注释生成于EF类)
例如:
namespace Requesto
{
public partial class Request_Comment : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
}
}