在 WPF 中绑定两个依赖属性
我声明了两个依赖属性: 首先,Color类型的FilterColor 第二个 FilterBrush 类型为 Brush。 当 FilterBrush.Color 属性更改时,我需要更新 FilterColor 的值;当 FilterColor 属性更改时,我需要更新 FilterBrush.Color 的值。 我怎样才能实现它?
I declare two Dependency properties:
first, FilterColor of type Color
and second FilterBrush of type Brush.
I need to update value of FilterColor when FilterBrush.Color property has changed, and I need to update value of FilterBrush.Color when FilterColor property has changed.
How I can realize it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 TwoWay 绑定绑定两个属性,如果您在 UI 中更改一个属性,则在属性设置器中更改另一个属性,反之亦然,并使用 INotifyPropertyChanged 通知您的 UI 该属性已更改。
Bind your two properties with TwoWay binding and if you change one in the UI change the other in the properties setter and Vice Versa, and use INotifyPropertyChanged to Notify your UI that the property changed.
您可以在 DependencyProperty 定义中执行此操作,也可以使用 DependencyPropertyDescriptor 在之后执行此操作
例如....
DependencyProperty Definition:
DependencyProperty Descriptor:
我可能有一些语法错误... 我没有编译器来执行此 操作检查代码
You can either do it in the DependencyProperty definition, or you can use a DependencyPropertyDescriptor to do it afterwards
For example....
DependencyProperty Definition:
DependencyProperty Descriptor:
I may have a few syntax errors... I don't have a compiler to check the code
您在哪里定义这些属性:在视图模型中还是在控件中?
如果它在视图模型中,您应该使用 INotifyPropertyChanged 而不是像这样的依赖属性:
如果它处于控制状态,请执行以下操作:
请注意,最后一种方法只是 hack,而且非常糟糕,我更喜欢第一种方法。
Where did you define these properties: in view model or in control?
If it's in view model you should use INotifyPropertyChanged instead of dependency properties like this:
If it is in control do this:
Note that last way is just hack and it is really bad way, I would prefer the first way.