NumericUpDown: CoerceValue Allows Binding to get out of sync

Mar 18, 2011 at 9:32 PM

Hello,

I was evaluating the NumericUpDown control for use in our project. In my evaluation, I believe I found a bug.

If you create a numeric up down control like the following:

<toolkit:NumericUpDown Value="{Binding Value}" SelectAllOnGotFocus="True" ValueType="{x:Type system:Int32}" Minimum="-10" Maximum="10" Increment="0.25" FormatString="0.##" />

(The Value property is a property on our View Model.)

When you enter a value outside of range, say 15, The Textbox will switch to 10, however the value of the binding will be 15. Thus, the value in the textbox and the value in the view model get out of sync.

This happens because the value in the view model is set before CoerceValue is called.

Mar 20, 2011 at 10:28 PM
Edited Mar 20, 2011 at 10:28 PM

Sorry for the late reply, I am out of country and haven't been near a computer.  First off, there are some problems with your XAML.  You set the ValueType to Int32, but your increment is a double of 0.25, and your format string also represents a double.

Now, to answer your question, this is not a bug in the NumericUpDown.  The Coerce only manipulates the value of the dependency property, not the binding source.  When a value changes, first the source is updated, then Coerce on the DP is executed, then propertyChanged on the DP.  This is the default behavior of DPs.  First thing I would be asking is how the valule got set to 15 if it is restricted to max 10 in the first place.  You should have validation logic on your properties to prevent that from happening.

Mar 21, 2011 at 1:12 PM

Thank you for your time. I will enforce it at the ViewModel level.

Apr 5, 2011 at 10:26 PM

After thinking about this some more, I believe that this should be considered a bug.  I will add the functionalty required to set the Value back to it's previous value if it does not fall between the min/max range.

Apr 6, 2011 at 4:19 PM

This has been fixed and should now work as expected.  You can get the fix in the latest source code download.