Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

DP Coercion and Binding in Wpf

$
0
0

I have been playing with CoerceValueCallback today and I realized Coercion of a DP while Binding to that same DP doesn't like each other at all. So my question is can you guys give me an example or an explaination of having a DependencyProperty that supports Coercion of itself while having a Binding in use at run time?

Why aren't coercec value and binding value equal to each other after a dependency property was coerced?

Here is an example:

Lets say there is a UserControl or CustomControl and that specific DependencyProperty is defined in one of them. Futhermore in XAML I use the DP with Binding to some random property from a simple ViewModel called "MySimpleViewModel".

Like I mentioned the DependencyProperty should coerce itself. What I mean by that is lets say the DP's type is int. Inside CoerceValueCallback you have a if value greater than 10, return 20 else return 5. So basically whatever value you give to the DependencyProperty it will adjust itself to either 20 or 5.

At the end the result should be something like this. Lets say I set the value on that random property from ViewModel is 3. The Binding would persist that value to DP at initalization time of the application. Subsequently DP's CoerceValueCallback gets executed and in there Coercing returns 5. Eventually 5 gets persisted back to ViewModels property over the Binding channel. This is not working though. The Binding doesn't get updated at all.

Can somebody help me out with this?

 





Viewing all articles
Browse latest Browse all 18858

Trending Articles