加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

wpf – 如何提高属性更改事件的依赖属性?

发布时间:2020-12-14 01:09:48 所属栏目:百科 来源:网络整理
导读:OK,所以我有这个控件有两个属性。其中一个是DependencyProperty,另一个是第一个的“别名”。我需要做的是在第一个更改时为第二个(别名)提高PropertyChanged事件。 注意:我使用DependencyObjects,而不是INotifyPropertyChanged(试过,没有工作,因为我的
OK,所以我有这个控件有两个属性。其中一个是DependencyProperty,另一个是第一个的“别名”。我需要做的是在第一个更改时为第二个(别名)提高PropertyChanged事件。

注意:我使用DependencyObjects,而不是INotifyPropertyChanged(试过,没有工作,因为我的控制是一个子类ListView)

这样的东西…..

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
    base.OnPropertyChanged(e);
    if (e.Property == MyFirstProperty)
    {
        RaiseAnEvent( MySecondProperty ); /// what is the code that would go here?
    }    
}

如果我使用INotify我可以这样做…

public string SecondProperty
{
    get
    {
        return this.m_IconPath;
    }
}

public string IconPath
{
    get
    {
        return this.m_IconPath;
    }
    set
    {
        if (this.m_IconPath != value)
        {
            this.m_IconPath = value;
        this.SendPropertyChanged("IconPath");
        this.SendPropertyChanged("SecondProperty");
        }
    }
}

其中我可以从一个setter对多个属性引发PropertyChanged事件。我需要能够做同样的事情,只使用DependencyProperties。

>在你的类中实现INotifyPropertyChanged。
>在注册依赖属性时,在属性元数据中指定回调。
>在回调中,提高PropertyChanged事件。

添加回调:

public static DependencyProperty FirstProperty = DependencyProperty.Register(
  "First",typeof(string),typeof(MyType),new FrameworkPropertyMetadata(
     false,new PropertyChangedCallback(OnFirstPropertyChanged)));

在回调中升级PropertyChanged:

private static void OnFirstPropertyChanged(
   DependencyObject sender,DependencyPropertyChangedEventArgs e)
{
   PropertyChangedEventHandler h = PropertyChanged;
   if (h != null)
   {
      h(sender,new PropertyChangedEventArgs("Second"));
   }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读