如何让PropertyChanged事件冒出来?

时间:2022-01-15 20:20:57

I have a hierarchy of objects (objects A, B), each of which implements INotifyPropertyChanged such that...

我有一个对象层次结构(对象A,B),每个对象都实现了INotifyPropertyChanged,这样......

A has a member of type B, B has a member of C, C is of type bool

A具有B型成员,B具有C成员,C是bool型

When C changes, its PropertyChanged event gets fired, however this does not fire B's property changed event. And therefore A does not react to B's change.

当C更改时,其PropertyChanged事件会被触发,但这不会触发B的属性更改事件。因此A不会对B的变化做出反应。

Is there a good way to bubble this up?

有没有一个好方法来搞砸这个?

2 个解决方案

#1


This is great - I have to spend some more time looking at it. Thank you.

这太棒了 - 我不得不花更多的时间来看待它。谢谢。

I found a different solution but it's a bit of a hack. I just set my binding path property to the nested type. Using my example above, in my xaml (which has a DataContext of object A) I set my binding as...

我发现了一个不同的解决方案,但它有点像黑客。我只是将绑定路径属性设置为嵌套类型。使用上面的例子,在我的xaml(它有一个对象A的DataContext)中我将绑定设置为...

{Binding Path=B.C}

Bubbling the event up is definitely more elegant.

冒泡活动肯定更优雅。

#2


This is a fairly simplistic solution but can't you just subscribe to the PropertyChanged event and propagate the call?

这是一个相当简单的解决方案,但您不能只订阅PropertyChanged事件并传播调用吗?

E.G.

MyContainedObject.PropertyChanged += PropertyChangedHandler;

and then in your handler:

然后在你的处理程序中:

private void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
{
    this.OnPropertyChanged(e.PropertyName);
}

This works great when your objects have properties with the same name. If you have properties with different names you will have to do a bit more work to convert between the property values.

当您的对象具有相同名称的属性时,这非常有用。如果您具有不同名称的属性,则必须在属性值之间进行更多工作。

private void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
{
    switch(e.PropertyName)       
    {         
     case "Property1":   
        this.OnPropertyChanged("ADifferentProperty1");
        break;                  
     case "Property2":            
        this.OnPropertyChanged("ADifferentProperty2");
        break;                   
     default:            
        this.OnPropertyChanged(e.PropertyName);           
        break;      
   }
}

#1


This is great - I have to spend some more time looking at it. Thank you.

这太棒了 - 我不得不花更多的时间来看待它。谢谢。

I found a different solution but it's a bit of a hack. I just set my binding path property to the nested type. Using my example above, in my xaml (which has a DataContext of object A) I set my binding as...

我发现了一个不同的解决方案,但它有点像黑客。我只是将绑定路径属性设置为嵌套类型。使用上面的例子,在我的xaml(它有一个对象A的DataContext)中我将绑定设置为...

{Binding Path=B.C}

Bubbling the event up is definitely more elegant.

冒泡活动肯定更优雅。

#2


This is a fairly simplistic solution but can't you just subscribe to the PropertyChanged event and propagate the call?

这是一个相当简单的解决方案,但您不能只订阅PropertyChanged事件并传播调用吗?

E.G.

MyContainedObject.PropertyChanged += PropertyChangedHandler;

and then in your handler:

然后在你的处理程序中:

private void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
{
    this.OnPropertyChanged(e.PropertyName);
}

This works great when your objects have properties with the same name. If you have properties with different names you will have to do a bit more work to convert between the property values.

当您的对象具有相同名称的属性时,这非常有用。如果您具有不同名称的属性,则必须在属性值之间进行更多工作。

private void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
{
    switch(e.PropertyName)       
    {         
     case "Property1":   
        this.OnPropertyChanged("ADifferentProperty1");
        break;                  
     case "Property2":            
        this.OnPropertyChanged("ADifferentProperty2");
        break;                   
     default:            
        this.OnPropertyChanged(e.PropertyName);           
        break;      
   }
}