gpt4 book ai didi

c# - WPF——自定义控件——继承DependencyProperty和PropertyChangedCallback

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:53 28 4
gpt4 key购买 nike

对于像下面这样的Custom Control,如何为继承的DependencyProperty IsEnabledProperty添加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 += …
}
}

但我不喜欢在每个实例中都注册事件处理程序的方法。所以我更喜欢元数据覆盖。

最佳答案

这个有效:

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

关于c# - WPF——自定义控件——继承DependencyProperty和PropertyChangedCallback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43543643/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com