将 dependencyProperty 添加到集合中?
我现在有一个继承基 Collection
类(dataGridRow 的 ViewModel)的类
,我想向此类添加 DependencyProperty
,以便我可以轻松绑定到它。问题是:Collection
不是DependencyObject
,所以我无法使用GetValue()
和SetValue()
方法,而且 C# 不进行多重继承,因此我可以继承 Collection
以及 DependencyObject
。
有一个简单的解决方案吗?
或者我别无选择,只能求助于简单的Property,继承INotifyPropertyChanged并实现PropertyChanged?
I have a class that inherits the base Collection
class (ViewModel for a dataGridRow)
now, I would like to add a DependencyProperty
to this class so that I can easily bind to it. problem is: Collection
is not a DependencyObject
so I cannot use the GetValue()
and SetValue()
methods, and C# doesn't do multiple inheritance so that I can inherit Collection
as well as DependencyObject
.
is there an easy solution for this?
or do I have no choice but to resort to a simple Property
, inherit INotifyPropertyChanged
and implement PropertyChanged
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恕我直言,ViewModel 应该永远实现 DependencyObject,而是实现 INotifyPropertyChanged(INPC)。
数据绑定到依赖属性确实比绑定到 INPC 更快,因为不涉及反射,但除非您正在处理大量数据,否则这不会成为问题。
实现 DependencyObject 严格针对 UI 元素,而不是其他任何东西,并且 DP 附带的基础设施不仅仅是更改通知。 ViewModel 类根据定义不是面向 UI 的,因此继承 DependencyObject 是一种设计味道。
IMHO ViewModel should never ever implement DependencyObject, but instead implement INotifyPropertyChanged(INPC).
DataBinding to Dependency Properties is indeed faster than Binding to INPC, since no reflection is involved, but unless you're dealing with sh*tloads of data, this won't be an issue.
Implemting DependencyObject is strictly for UI elements, not for anything else, and the infrastructure that comes with DP is a lot more than just change notification. ViewModel classes are not UI oriented by definition, and hence inheriting DependencyObject is a design smell.
使用聚合而不是多重继承:
Use aggregation instead of multi-inheritance:
只是为了结束这个问题,我会满足于:“不,这是不可能的”。
亚历克斯的回答提供了一个有趣的观点,但就我而言,正如我的评论中所述,这不会使任何事情变得更容易或更易读。
最后我实现了INPC
just for the sake of closing this question, I'll settle for: "no, this isn't possible".
alex's answer offers an interesting perspective, but in my case and as stated in my comment, this would not make anything easier or more readable.
in the end, I implemented INPC