如何提高对一个依赖属性改变性质的事件? [英] How To Raise Property Changed events on a Dependency Property?

查看:126
本文介绍了如何提高对一个依赖属性改变性质的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我有两个属性此控件。其中之一是一个的DependencyProperty,另一个是一个别名的第一个。我需要能够做的是提高PropertyChanged事件,用于当第一个被改变的第二个(别名)

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

这样的事情......

 保护覆盖无效OnPropertyChanged(DependencyPropertyChangedEventArgs E)
{
    base.OnPropertyChanged(E);
    如果(e.Property == MyFirstProperty)
    {
        RaiseAnEvent(MySecondProperty); ///什么是code,它会去吗?
    }
}

如果我是用我的inotify可以做这样的...

 公共字符串SecondProperty
{
    得到
    {
        返回this.m_IconPath;
    }
}公共字符串IconPath
{
    得到
    {
        返回this.m_IconPath;
    }
    组
    {
        如果(this.m_IconPath!=值)
        {
            this.m_IconPath =价值;
        this.SendPropertyChanged(IconPath);
        this.SendPropertyChanged(SecondProperty);
        }
    }
}

在那里我可以提高从一个二传手的多个属性的PropertyChanged事件。我需要能够做同样的事情,只能用DependencyProperties。


解决方案

  1. 实施 INotifyPropertyChanged的在你的类。


  2. 在注册依赖属性指定属性的元数据的回调。


  3. 在回调,提高PropertyChanged事件。


添加回调:

 公共静态的DependencyProperty FirstProperty = DependencyProperty.Register(
  第一,
  typeof运算(字符串),
  typeof运算(的MyType)
  新FrameworkPropertyMetadata(
     假,
     新PropertyChangedCallback(OnFirstPropertyChanged)));

募集的PropertyChanged 在回调:

 私有静态无效OnFirstPropertyChanged(
   DependencyObject的发件人,DependencyPropertyChangedEventArgs E)
{
   PropertyChangedEventHandler H =的PropertyChanged;
   如果(H!= NULL)
   {
      H(发件人,新PropertyChangedEventArgs(二));
   }
}

OK, so I have this control with two properties. One of these is a DependencyProperty, the other is an "alias" to the first one. What I need to be able to do is raise the PropertyChanged event for the second one (the alias) when the first one is changed.

NOTE: I am using DependencyObjects, not INotifyPropertyChanged (tried that, didn't work because my control is a sub-classed ListView)

something like this.....

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

If I were using an INotify I could do like this...

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

where I can raise PropertyChanged events on multiple properties from one setter. I need to be able to do the same thing, only using DependencyProperties.

解决方案

  1. Implement INotifyPropertyChanged in your class.

  2. Specify a callback in the property metadata when you register the dependency property.

  3. In the callback, raise the PropertyChanged event.

Adding the callback:

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

Raising PropertyChanged in the callback:

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

这篇关于如何提高对一个依赖属性改变性质的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆