wpf 在生成的实体框架类上实现 INotifyPropertyChanged
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14132349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implement INotifyPropertyChanged on generated Entity Framework classes
提问by Steve
I have an SQL DB and am implementing a WPF UI to update it. If I use EF5 to generate the classes from the DB, how can I implement INotifyPropertyChanged on the generated classes and properties so I can easily bind to them with the UI? Is there an easy way to achieve this?
我有一个 SQL DB 并且正在实现一个 WPF UI 来更新它。如果我使用 EF5 从数据库生成类,我如何在生成的类和属性上实现 INotifyPropertyChanged 以便我可以轻松地使用 UI 绑定到它们?有没有简单的方法来实现这一目标?
Thanks
谢谢
采纳答案by WildCrustacean
If you follow the recommended MVVMpattern for WPF, you can treat your generated classes as the Model, and then write ViewModel wrappers that implement INotifyPropertyChanged. The ViewModel classes would access your DB classes and expose properties that you can bind to your UI in XAML.
如果遵循WPF推荐的MVVM模式,则可以将生成的类视为模型,然后编写实现 INotifyPropertyChanged 的 ViewModel 包装器。ViewModel 类将访问您的 DB 类并公开您可以在 XAML 中绑定到您的 UI 的属性。
As noted in your comment, this can result in a lot of work writing boilerplate code, but there are some ways to deal with that. See this questionfor some ideas.
正如您在评论中所指出的,这可能会导致编写样板代码的大量工作,但有一些方法可以解决这个问题。有关一些想法,请参阅此问题。
While more work at first, the MVVM pattern can definitely pay off in the long run if you ever need to do any intermediate formatting or processing, or if you need to change your database classes without affecting the UI.
虽然一开始需要做更多的工作,但从长远来看,如果您需要进行任何中间格式化或处理,或者如果您需要在不影响 UI 的情况下更改数据库类,MVVM 模式绝对可以带来回报。
回答by WildCrustacean
I needed to do the same recently but with Winforms. If you don't want to follow the MVVM pattern as suggested by bde you can modify the t4 template to implement INotifyPropertyChanged on your generated entities.
我最近需要做同样的事情,但使用 Winforms。如果您不想遵循 bde 建议的 MVVM 模式,您可以修改 t4 模板以在生成的实体上实现 INotifyPropertyChanged。
This answer helped me: https://stackoverflow.com/a/12192358/1914530
这个答案对我有帮助:https: //stackoverflow.com/a/12192358/1914530
回答by mark_h
There's a NuGet package called PropertyChanged.Fody that makes adding INotifyPropertyChanged to a classes properties really simple. Once you've installed the package just add the [ImplementPropertyChanged] attribute to any class or partial class and the package will add INotifyPropertyChanged for you.
有一个名为 PropertyChanged.Fody 的 NuGet 包,它使得将 INotifyPropertyChanged 添加到类属性非常简单。安装包后,只需将 [ImplementPropertyChanged] 属性添加到任何类或部分类,包将为您添加 INotifyPropertyChanged。
Here's a simple example of it's use;
这是它使用的一个简单示例;
using PropertyChanged;
[ImplementPropertyChanged]
public partial class Order
{
}
See GitHubfor more information.
有关更多信息,请参阅GitHub。
回答by Yet Another Code Maker
Make all your EF generated classes inherit from a class which implements the INotifyPropertyChangedinterface (using the non-generated partial classes you probably already defined). Add a method to this base class, which raises the PropertyChangedevent with an empty string as PropertyName. Then, every time you call this method on an EF generated class instance, all its modified properties will be refreshed in your WPF UI.
使所有 EF 生成的类继承自实现INotifyPropertyChanged接口的类(使用您可能已经定义的非生成部分类)。向该基类添加一个方法,该方法会引发PropertyChanged带有空字符串的事件PropertyName。然后,每次在 EF 生成的类实例上调用此方法时,其所有修改的属性都将在 WPF UI 中刷新。
回答by Todor Mitev
You can edit the EF template (.tt file) to generate the propertyChanged stuff on your properties, or (a bit risky:)) to edit the generated classes. The last is a bit risky, because if you regenerate the model, all changes will be lost. So maybe the variant with the wrapper classes or the template editing (a bit hard :S) are the best.
您可以编辑 EF 模板(.tt 文件)以在您的属性上生成 propertyChanged 内容,或者(有点冒险:))来编辑生成的类。最后一个有点冒险,因为如果重新生成模型,所有更改都将丢失。所以也许带有包装类或模板编辑的变体(有点难:S)是最好的。

