WPF TextBox限制只能输入数字的两种方法

MainWindow.xaml:

<Window x:Class="wpfcore.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:wpfcore" 
        xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
        mc:Ignorable="d"
        Background="#2D2D30"
        SnapsToDevicePixels="True"
        FontSize="18"
        UseLayoutRounding="True"
        Title="MainWindow" Width="820" Height="340">
    <StackPanel>
        <!--第一种方法,直接设置禁用输入法,并添加PreviewTextInput事件,如果不满足条件,就禁止输入-->
        <TextBox InputMethod.IsInputMethodEnabled="False" PreviewTextInput="TextBox_PreviewTextInput" Margin="10"/>
        <!--第二种方法,写一个附加属性,在属性改变时,给textbox添加上相应事件,这个写完后,复用时就方便喽-->
        <TextBox local:TextBoxAttachedProperties.IsOnlyNumber="true" Margin="10"/>
    </StackPanel>
</Window>

MainWindow.cs:

using System.Text.RegularExpressions;
using System.Windows;

namespace wpfcore
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

        }
        private void TextBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
        }
    }

}

第二种方法:
新建一个TextBoxAttachedProperties.cs文件,定义附加属性:

using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace wpfcore
{
    public class TextBoxAttachedProperties
    {
        public static bool GetIsOnlyNumber(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsOnlyNumberProperty);
        }
        public static void SetIsOnlyNumber(DependencyObject obj, bool value)
        {
            obj.SetValue(IsOnlyNumberProperty, value);
        }
        public static readonly DependencyProperty IsOnlyNumberProperty =
            DependencyProperty.RegisterAttached("IsOnlyNumber", typeof(bool), typeof(TextBox), new PropertyMetadata(false,
                (s, e) =>
                {
                    if (s is TextBox textBox)
                    {
                        textBox.SetValue(InputMethod.IsInputMethodEnabledProperty, !(bool)e.NewValue);
                        textBox.PreviewTextInput -= TxtInput;
                        if (!(bool)e.NewValue)
                        {
                            textBox.PreviewTextInput += TxtInput;
                        }
                    }
                }));
        private static void TxtInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值