Skip to content

NumericUpDown在VerifyFunc校验不通过的情况下无法正常更新显示的值 #393

@seedtyx

Description

@seedtyx

NumericUpDown在VerifyFunc校验不通过的情况下无法正常更新显示的值

现象

使用数值选择框控件时,当触发VerifyFunc校验,且校验结果为Failed时,后台再次给控件的value赋值,前台控件输入框不更新。

重现问题

XAML

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <hc:NumericUpDown x:Name="nud" Style="{StaticResource NumericUpDownPlus}"/>
        <Button Content="Test" Click="Button_Click"/>
    </StackPanel>
</Window>

XAML.CS

using HandyControl.Data;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            nud.VerifyFunc = value => 
            {
                if (double.TryParse(value,out double d) && d < 10)
                    return OperationResult.Failed("测试测试");
                return OperationResult.Success();
            };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            nud.Value = 10;
        }
    }
}

操作步骤

  1. 先点击按钮,控件赋值为合法值10。
  2. 手动输入一个非法值,例如2,此时界面显示红字提示。
  3. 再次点击按钮,控件无法显示合法值10。但断点可看到控件值已经为10。

原因

查源码后发现,校验结果为Failed后,value仍为上一次的合法值10。输入非法值不会更新value属性。所以第二次点击按钮不会触发Value属性的PropertyChanged。

修改方法

在CorceValueCallBack中设置Text,原因是当使用绑定时,属性的Set访问器不会被调用,PropertyChanged不一定触发(当值相同时),目前只想到在CorceValue里更新,如有更好的解决方法,还请告知,谢谢。见 PullRequest #395

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions