将 dependencyProperty 添加到集合中?

发布于 2024-10-25 08:32:38 字数 425 浏览 5 评论 0原文

我现在有一个继承基 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感情旳空白 2024-11-01 08:32:38

恕我直言,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.

你是我的挚爱i 2024-11-01 08:32:38

使用聚合而不是多重继承:

class MyCollection<T> : DependencyObject, ICollection<T>
{
    // Inner collection for call redirections. 
    private Collection<T> _collection; 

    #region ICollection<T> Members

    public void Add(T item)
    {
        this._collection.Add(item);
    }

    public void Clear()
    {
        this._collection.Add(clear);
    }

    // Other ICollection methods ... 
    #endregion

    #region MyProperty Dependency Property

    public int MyProperty
    {
        get
        {
            return (int)this.GetValue(MyCollection<T>.MyPropertyProperty);
        }
        set
        {
            this.SetValue(MyCollection<T>.MyPropertyProperty, value);
        }
    }

    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty",
            typeof(int),
            typeof(MyCollection<T>),
            new FrameworkPropertyMetadata(0));

    #endregion
}

Use aggregation instead of multi-inheritance:

class MyCollection<T> : DependencyObject, ICollection<T>
{
    // Inner collection for call redirections. 
    private Collection<T> _collection; 

    #region ICollection<T> Members

    public void Add(T item)
    {
        this._collection.Add(item);
    }

    public void Clear()
    {
        this._collection.Add(clear);
    }

    // Other ICollection methods ... 
    #endregion

    #region MyProperty Dependency Property

    public int MyProperty
    {
        get
        {
            return (int)this.GetValue(MyCollection<T>.MyPropertyProperty);
        }
        set
        {
            this.SetValue(MyCollection<T>.MyPropertyProperty, value);
        }
    }

    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty",
            typeof(int),
            typeof(MyCollection<T>),
            new FrameworkPropertyMetadata(0));

    #endregion
}
摇划花蜜的午后 2024-11-01 08:32:38

只是为了结束这个问题,我会满足于:“不,这是不可能的”。

亚历克斯的回答提供了一个有趣的观点,但就我而言,正如我的评论中所述,这不会使任何事情变得更容易或更易读。

最后我实现了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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文