且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF:不能绑定到用户控件中的依赖项属性

更新时间:2022-05-02 18:00:19

dependency属性已经可以处理更改通知,因此您无需显式实现INotifyPropertyChanged。
因此,您可以从设置器中删除 OnPropertyChanged( Header);

The dependency property already handles notifying of changes so you don't explicitly have to implement INotifyPropertyChanged. So you can remove the OnPropertyChanged("Header"); from the setter

调用此属性更改的函数的正确方法是在Dependency属性中定义它:

If you wanted to call a function on the change of this property the correct way is to define it in the Dependency property:

 public static DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(String), typeof(myItem), new PropertyMetadata("Test", OnHeaderChanged));

    public String Header
        {
            get
            {
                return (String)GetValue(HeaderProperty);
            }
            set
            {
                SetValue(HeaderProperty, value);
            }
        }

    private void OnHeaderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){ //do something}