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:
- Is this possible?
- If it’s not possible is there an alternate implementation you can suggest?
It is not possible.
DependencyPropertycan only be created in class that extendsDependencyObject.From MSDN-DependendyObject
Not Possible
Use INotifyPropertyChanged, and IDataErrorInfo with normal CLR property for Property validation. Example