简体   繁体   English

如何使用Binding with Dependency Property链接属性

[英]How to chain properties using Binding with Dependency Property

Assume the following data-flow happens using Binding: 假设使用Binding发生以下数据流:

PropertyA <--> DependencyProperty <--> PropertyB
  • The <--> marks represents a TwoWay binding mode. < - >标记表示TwoWay绑定模式。
  • Both PropertyA and PropertyB classes implement INotifyPropertyChanged and invoke OnPropertyChanged method in their setter accessor when changed. PropertyAPropertyB类在更改时实现INotifyPropertyChanged并在其setter访问器中调用OnPropertyChanged方法。

The binding compiler setting starts from the above flow left side to the right, that means the last binding set is between DependencyProperty and PropertyB . 绑定编译器设置从上面的流程左侧开始向右,这意味着最后一个绑定集在DependencyPropertyPropertyB之间。

The problem is when PropertyA notifies for property changed, DependencyProperty doesn't invoke her OnPropertyChangedCallback method. 问题是当PropertyA通知属性发生变化时, DependencyProperty不会调用她的OnPropertyChangedCallback方法。

On the contrary, when PropertyB notifies for property changed, DependencyProperty DOES invoke her OnPropertyChangedCallback method. 相反,当PropertyB通知属性发生更改时, DependencyProperty调用她的OnPropertyChangedCallback方法。

I would like to know if it even possible to achieve such behavior using the .NET Binding mechanism, or there is another way. 我想知道是否有可能使用.NET Binding机制实现这种行为,或者还有另一种方法。

Thanks in advance. 提前致谢。

UPDATE UPDATE

The term "chain" in the question context, for example, means: 例如,问题上下文中的术语“链”意味着:

when PropertyA notifies for a change, PropertyB will be notified from DependencyProperty which notified from PropertyA change, and vice versa. PropertyA通知变更时, PropertyB将从DependencyProperty通知PropertyA变更通知,反之亦然。

The above example "chains" between PropertyA to PropertyB using DependencyProperty . 上面的示例使用DependencyPropertyPropertyAPropertyB之间“链接”。

You can't have two bindings on the same DependencyProperty being the target. 不能在作为目标的同一DependencyProperty上有两个绑定

It would mean that you 'd have two sources for the same DependencyProperty . 这意味着你有两个相同的DependencyProperty源。

But you can have a property (on the Model or ViewModel) being the source for many dependency properties . 但是您可以拥有一个属性 (在Model或ViewModel上)作为许多依赖项属性 That's pretty common. 这很常见。

It seems normal to me if you ve got the OnPropertyChangedCallback called only when one property is changed. 如果只有在更改一个属性时才调用OnPropertyChangedCallback这对我来说似乎很正常。

There can be only one source, and the last binding wins. 只能有一个源,最后一个绑定获胜。

Here is a nicepage with schemas : http://blog.scottlogic.com/2012/04/05/everything-you-wanted-to-know-about-databinding-in-wpf-silverlight-and-wp7-part-one.html 这是一个带有模式的漂亮页面: http//blog.scottlogic.com/2012/04/05/everything-you-wanted-to-know-about-databinding-in-wpf-silverlight-and-wp7-part-one html的

EDIT 编辑

If you want to have two properties of two objects in phase, you can use ViewModel as an intermediary between A holding PropB and B holding PropB. 如果要同时拥有两个对象的两个属性,可以使用ViewModel作为A持有PropB和持有PropB的B之间的中介。

So you'd have : | 所以你有:| <--> ClassA.PropA View <---> ViewModel.Prop-| < - > ClassA.PropA View <---> ViewModel.Prop- | | | <--> ClassB.PropB < - > ClassB.PropB

It means : - ViewModel Object would register to the PropertyEventChanged of ClassA and ClassB. 这意味着: - ViewModel Object将注册到ClassA和ClassB的PropertyEventChanged。
ViewModel would raise an event if ClassA or ClassB would raise an event for PropA or PropB change. 如果ClassA或ClassB将为PropA或PropB更改引发事件,ViewModel将引发事件。

  • ViewModel would call ClassA.PropA.set and ClassB.PropB.set when ViewModel Prop.set would be called. 当调用ViewModel Prop.set时,ViewModel将调用ClassA.PropA.set和ClassB.PropB.set。

  • When get of ViewModel, only ClassA.PropA would be read. 获取ViewModel时,只会读取ClassA.PropA。 But it wouldn't matter because PropA and PropB are worth the same. 但这并不重要,因为PropA和PropB值得一试。

Hope it helps, regards 希望它有所帮助,问候

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM