简体   繁体   English

如何绑定到密封类?

[英]How can I Databind to a Sealed Class?

I'm trying to bind some WPF controls to a sealed class provided to me. 我试图将一些WPF控件绑定到提供给我的密封类上。 Because it is sealed, I cannot inherit from it to create a class that implements INotifyPropertyChanged. 因为它是密封的,所以我不能从它继承来创建实现INotifyPropertyChanged的类。 So I'm not sure how I should go about doing this. 所以我不确定该怎么做。 Should I create a wrapper that implements INotifyPropertyChanged? 我应该创建实现INotifyPropertyChanged的包装器吗? Anyone have any tips on what to do? 任何人都该怎么办?

Wrapping the class in a view model that provides notification (and maybe validation, editing support etc.) is the usual approach. 将类包装在提供通知(可能还有验证,编辑支持等)的视图模型中是通常的方法。 The question is whether the underlying object is modified elsewhere in your process and, if so, whether you want those changes to be automatically reflected in the UI. 问题是是否在流程的其他地方修改了基础对象,如果是,是否希望这些更改自动反映在UI中。 If you do, you'll need a more centralized approach - usually by way of a service that manages those objects. 如果这样做,您将需要一种更集中的方法-通常通过管理那些对象的服务来实现。

Hmm, I would put a wrapper around it. 嗯,我会在上面放一个包装纸。 It's like the approach in MVVM. 就像MVVM中的方法一样。 The Model is wrapped by the ViewModel . ModelViewModel包装。

如果绑定仅用于读取,则可以不执行INotifyPropertyChanged而仅拥有绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM