Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
seedtyx opened this issue Jun 8, 2020 · 0 comments · Fixed by #395
Closed
Labels
🐛 bug Something isn't working

Comments

@seedtyx
Copy link
Contributor

seedtyx commented Jun 8, 2020

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

seedtyx added a commit to seedtyx/HandyControl that referenced this issue Jun 8, 2020
@seedtyx seedtyx mentioned this issue Jun 8, 2020
@seedtyx seedtyx mentioned this issue Jun 8, 2020
@seedtyx seedtyx changed the title NumericUpDown在某一情况下无法正常更新显示的值 NumericUpDown在VerifyFunc校验不通过的情况下无法正常更新显示的值 Jun 15, 2020
@ghost1372 ghost1372 added the 🐛 bug Something isn't working label Jun 16, 2020
@NaBian NaBian closed this as completed in c9920ba Jun 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants