Reputation: 537
In the case of the Custom Control like the following, how to add PropertyChangedCallback for inherited DependencyProperty IsEnabledProperty?
public class MyCustomControl : ContentControl
{
// Custom Dependency Properties
static MyCustomControl ()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
// TODO (?) IsEnabledProperty.OverrideMetadata(typeof(MyCustomControl), new PropertyMetadata(true, CustomEnabledHandler));
}
public CustomEnabledHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// Implementation
}
}
Yes, there is another option like listen to the IsEnabledChangeEvent
public class MyCustomControl : ContentControl
{
public MyCustomControl()
{
IsEnabledChanged += …
}
}
But I don't like the approach register event handler in every instance. So I prefer the metadata overriding.
Upvotes: 1
Views: 1349
Reputation: 128098
This works:
static MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl),
new FrameworkPropertyMetadata(typeof(MyCustomControl)));
IsEnabledProperty.OverrideMetadata(typeof(MyCustomControl),
new FrameworkPropertyMetadata(IsEnabledPropertyChanged));
}
private static void IsEnabledPropertyChanged(
DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("{0}.IsEnabled = {1}", obj, e.NewValue);
}
Upvotes: 3