在自定义控件像下面这样的情况下,如何添加PropertyChangedCallback为继承的DependencyProperty IsEnabledProperty?WPF - 自定义控制 - 继承的DependencyProperty和PropertyChangedCallback
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
}
}
是的,有喜欢的另一种选择听IsEnabledChangeEvent
public class MyCustomControl : ContentControl
{
public MyCustomControl()
{
IsEnabledChanged += …
}
}
但我不喜欢在每一个实例中的方法注册事件处理程序。所以我更喜欢元数据覆盖。
OverrideMetadata有什么问题?但请注意,它应该是FrameworkPropertyMetadata而不是PropertyMetadata。 – Clemens
@Clemens如果我在** XAML **中使用这个控件,我会收到错误:_Metadata覆盖和基础元数据必须是相同类型或派生类型._我也使用'FrameworkPropertyMetadata'尝试它。 – David
它适用于FrameworkPropertyMetadata。再试一次。 – Clemens