C# 向扩展ObservableCollection的类添加DependencyProperty

C# 向扩展ObservableCollection的类添加DependencyProperty,c#,wpf,dependency-properties,observablecollection,dependencyobject,C#,Wpf,Dependency Properties,Observablecollection,Dependencyobject,我有一个名为ObservaleCollectionWithValidState的类,当其子对象的某个对象违反验证规则时,它会通知自己 类别: child child <== violated a passed in Predicate and is now invalid. child 我的两个问题: 这可能吗 如果不可能,您是否可以建议其他实现方案 这是不可能的DependencyProperty只能在扩展DependencyObject的类中创建 从 DependencyObjec

我有一个名为
ObservaleCollectionWithValidState
的类,当其子对象的某个对象违反验证规则时,它会通知自己

类别:

child
child  <== violated a passed in Predicate and is now invalid.
child
我的两个问题:

  • 这可能吗
  • 如果不可能,您是否可以建议其他实现方案

  • 这是不可能的
    DependencyProperty
    只能在扩展
    DependencyObject
    的类中创建

    DependencyObject服务和特性包括以下内容: 依赖属性托管支持

    • 通过调用register方法并将该方法的返回值存储为类中的公共静态字段,可以注册依赖项属性

    这可能吗

    不可能

    如果不可能,您是否可以建议其他实现方案


    使用、和与普通CLR属性一起进行属性验证

    不幸的是,您不能从多个基类派生,因此我认为这种方法行不通,也许您可以将DependencyProperty和集合包装在从Dependency对象派生的新类中,并使用collection changed事件来处理验证。不确定这是否适用于您的场景。我最终使用了INotifyPropertyChanged…如果能够作为dep道具访问它会很好,但我做到了。
    public class ObservableCollectionWithIsValidState<T> : ObservableCollection<T> where T : INotifyPropertyChanged,
    {
        public static readonly DependencyProperty IsValidPropertyProperty = DependencyProperty.Register("IsValid", typeof(bool), typeof(ObservableCollectionWithIsValidState<T>), new PropertyMetadata(true));
    
        public bool IsValid
        {
           get { return (bool)GetValue(IsValidPropertyProperty); }
           set { SetValue(IsValidPropertyProperty, value); }
        }
    }