Question

I have a class called ObservableCollectionWithValidState that serves to notify itself when an of it's child objects break a validation rule.

Class:

child
child  <== violated a passed in Predicate and is now invalid.
child

When this happens I would love to have a DependencyProperty on this class that I can set which can be bound to.

The problem is that my class extends ObservableCollection<T> but I can't see how to get DependencyObject into the picture.

I am pasting the initial declaration of the class along with an example of the property I would like to add (this will not work unless I can extend DependencyProperty).

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); }
    }
}

My two questions:

  1. Is this possible?
  2. If it's not possible is there an alternate implementation you can suggest?
Était-ce utile?

La solution

It is not possible. DependencyProperty can only be created in class that extends DependencyObject.

From MSDN-DependendyObject

DependencyObject services and characteristics include the following: Dependency property hosting support.

  • You register a dependency property by calling the Register method, and storing the method's return value as a public static field in your class.

Is this possible?

Not Possible

If it's not possible is there an alternate implementation you can suggest?

Use INotifyPropertyChanged, and IDataErrorInfo with normal CLR property for Property validation. Example

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top