WPF Nested DependencyProperty Value Coercion

I've built a UserControl for selecting a Color with a DependencyProperty for the selected color, and a CoerceValueCallback that ensures that selected colors are fully opaque and fully bright. If I add this control to a window, and bind the background color of the window to the selected color property of the control, everything works as I expect. I can push "bad" colors into the control using a button, but the value coercion logic ensures that "valid" colors are stored instead, and both the control and the window background display the "valid" colors instead of the "bad" colors that were pushed in.

However, if I try to use this control within another UserControl that has its own DependencyProperty for selected color, and use a two-way binding to connect the two together, the value coercion logic doesn't work as I expect. The outer control keeps the "bad" colors that are pushed into it, regardless of any value coercion that happens on the inner control.

Is there a way to force coerced values from the inner control to sync back to the outer control without having to re-implement the value coercion logic on the outer control as well?


I don't think it is possible to force the coerced value to sync back. WPF keeps track of the binding update operation and prevents actions that could lead to unwanted and/or unexpected behavior. And syncing back definitely has a potential to make things go really bad , eg if your outer control would implement coercion logic that would allow a set of values disjoint with the set of values allowed by the inner control you would end up caught in an endless loop of forcing the value back and forth.

Now there are several approaches to address your problem:

1.

Rather than using coercion mechanism you could check the value in the property changed callback in your inner control and set it again if the original value was not valid. If I recall correctly though due to the said WPF safety mechanism (property changes while executing that property's change callback are ignored) you'd have to use a Dispatcher , but I might be wrong on that. Nevertheless I'd not recommend this approach.

2.

If the purpose of the outer control property is just to expose the inner control property, your best bet would be to use DependencyProperty.AddOwner method rather than the static DependencyProperty.Register method to register the property on the outer control.

3.

Otherwise you could extract the coercion logic to a static method and simply use it for both inner and outer control's property coercion. That way you'd not need to re-implement the logic.

链接地址: http://www.djcxy.com/p/67576.html

上一篇: 如何在SDL 2中获取并保存BMP截图?

下一篇: WPF嵌套依赖属性值强制