Int32EditBox Min/Max Coercion Causing Grief

Editors for WPF Forum

Posted 4 months ago by Daniel Ashby
Version: 21.1.3
Platform: .NET 6.0
Environment: Windows 11 (64-bit)
Avatar

I set up a view bound to a viewmodel something like this:

<editors:Int32EditBox Value="{Binding AwesomeThing}" Maximum="{Binding Max}" />

The bound class uses INotifyDataErrorInfo to validate input and there is a style that displays the validation 

<Style TargetType="editors:Int32EditBox">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="Background" Value="Pink" />
            <Setter Property="FrameworkElement.ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
        </Trigger>
    </Style.Triggers>
</Style>

Both the value and the max are sometimes updated by other code. If they are updated in such a way that the AwesomeThing is greater than the Max, the editbox will run some coercion logic and display Max instead of AwesomeThing. This is a problem because now my validation is showing an error indicating an invalid value, but the control is showing a valid value.

Is there a way to turn off this coercion? I want to keep the max value in place because it makes user input nice, but I very much need the editbox to show the current value of the bound property even when it is outside the min/max.

[Modified 4 months ago]

Comments (2)

Posted 4 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Daniel,

The Int32EditBox.Value dependency property has a coerce method configured on it that will invoke the protected virtual Int32EditBox.CoerceValidValue method.  Our logic there will ensure the Value is within Min/Max if the Value is non-null. 

You could probably override that CoerceValidValue method and only call the base coerce if focus is within the control.  That assumes that your external update only happens when focus is outside of the control.  Or if you had some global flag in your app that is set when updating values, you could check for that in a CoerceValidValue override and only call the base coerce when that flag is not set.


Actipro Software Support

Posted 3 months ago by Daniel Ashby
Avatar

Tested that today, it works great! Many thanks.

The latest build of this product (v24.1.2) was released 9 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.