wpf toolkit integer updown does not retain value on lostfocus
-
26-02-2021 - |
Question
I have an integer updown wpf toolkit control. I am able to change the values using the arrow keys and able to type it explicitly. But after changing the value, if I move focus away the value become zero.
Any help would be appreciated. Thanks!
<wpftlkit:IntegerUpDown Grid.Column="1" Margin="6" VerticalAlignment="Center" Increment="1" Maximum="7"
Value="{Binding ValueH, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Style="{StaticResource StyleErrorTemplate}"
IsEnabled="{Binding IsEnabled}" Visibility="{Binding Visibility}" />
Solution
I suspect the issue is a binding issue. The actual value doesn't get updated until you lose focus (due to your UpdateSourceTrigger), at which point a broken binding or binding to a property that forces the value back to zero would give this behavior.
OTHER TIPS
This is a the way Toolkit works. To change value to maximum or minimum when user types in the value that is out of Min-Max bounds, you should download WPF Toolkit sources (http://wpftoolkit.codeplex.com/SourceControl/changeset/view/98195) and change file CommonNumericUpdown.cs in folder Xceed.Wpf.Toolkit/NumericUpDown/NumericUpDown like is given below. Note the lines before the exceptions are throwed, they are added.
private void ValidateDefaultMinMax( T? value )
{
// DefaultValue is always accepted.
if( object.Equals( value, DefaultValue ) )
return;
if (IsLowerThan(value, Minimum))
{
Value = Minimum;
throw new ArgumentOutOfRangeException("Minimum", String.Format("Value must be greater than MinValue of {0}", Minimum));
}
else if (IsGreaterThan(value, Maximum))
{
Value = Maximum;
throw new ArgumentOutOfRangeException("Maximum", String.Format("Value must be less than MaxValue of {0}", Maximum));
}
}