【问题标题】:TextBox/NumberBox Filtering WINUI3文本框/数字框过滤 WINUI3
【发布时间】:2021-09-07 20:31:56
【问题描述】:

是否有可能实现一个 TextBox/NumberBox 来实时过滤并只接受数值,就像 NumberBox 通常所做的那样,但不仅仅是在用户完成输入时,如果可以,我该怎么做?

【问题讨论】:

    标签: .net xaml uwp winui winui-3


    【解决方案1】:

    一般方法是处理BeforeTextChanging 事件:

    private void TextBox_BeforeTextChanging(TextBox sender, TextBoxBeforeTextChangingEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.NewText))
            foreach (char c in e.NewText)
                if (!char.IsDigit(c))
                {
                    e.Cancel = true;
                    return;
                }
    }
    

    XAML:

    <TextBox BeforeTextChanging="TextBox_BeforeTextChanging" />
    

    【讨论】:

    • 属性 BeforeTextChanging 不存在。
    • 我不是财产。这是一个事件,它确实存在:docs.microsoft.com/en-us/uwp/api/…
    • @Servidor:你试过这个还是发生了什么?
    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    相关资源
    最近更新 更多