当前位置:网站首页>WPF textbox limits two ways to enter numbers only
WPF textbox limits two ways to enter numbers only
2022-07-22 19:49:00 【Caiyuan who loves programming】
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>
<!-- The first method , Set disable input method directly , And add PreviewTextInput event , If you don't do that , No input -->
<TextBox InputMethod.IsInputMethodEnabled="False" PreviewTextInput="TextBox_PreviewTextInput" Margin="10"/>
<!-- The second method , Write an additional attribute , When properties change , to textbox Add the corresponding event , When this is finished , It is convenient to reuse -->
<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);
}
}
}
The second method :
Create a new one TextBoxAttachedProperties.cs file , Define additional properties :
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);
}
}
}
边栏推荐
猜你喜欢
随机推荐
HTB- Armageddon
zabbix怎样自定义mysql监控项并触发告警
js map与foreach区别
Atr5179 single pole double throw switch chip replaces as179-92lf
撞库、脱库和洗库
It is found that the MariaDB database is 12 hours late, and the xxljob scheduled task scheduling is abnormal
SVN服务端与客户端安装(汉化包)以及简单使用
NiO character set and charset
No code coverage driver is available
oracle怎样转义单引号
Neo4j - Cypher 语法示例
C#正则表达式提取指定词语出现的索引位置
Polymorphism
派生类的构造函数和析构函数
How Oracle escapes single quotation marks
NIOBuffer(缓冲器)
How maxscale handles the state of event after MariaDB master-slave switch -handle_ events
08.01 邻接矩阵
[learn C and fly] Chapter 3 branch structure (exercise 3.1 simple guessing game)
Static member