WPF 数字输入控件NumberTextBox

[TemplatePart(Name = "InButton",Type = typeof(Button))] [TemplatePart(Name = "DeButton", Type = typeof(Button))] public class NumberTextBox : TextBox { static NumberTextBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(NumberTextBox), new FrameworkPropertyMetadata(typeof(NumberTextBox))); } public int Step { get { return (int)GetValue(StepProperty); } set { SetValue(StepProperty, value); } } public static readonly DependencyProperty StepProperty = DependencyProperty.Register("Step", typeof(int), typeof(NumberTextBox), new PropertyMetadata(1)); public int Minimum { get { return (int)GetValue(MinimumProperty); } set { SetValue(MinimumProperty, value); } } public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(int), typeof(NumberTextBox), new PropertyMetadata(0)); public NumberTextBox() { InputMethod.SetIsInputMethodEnabled(this, false); this.TextChanged += NumberTextBox_TextChanged; } private void NumberTextBox_TextChanged(object sender, TextChangedEventArgs e) { if (!int.TryParse(this.Text, out int _)) { this.Text = ""; e.Handled = true; } } public override void OnApplyTemplate() { base.OnApplyTemplate(); Button DeButton = (Button)this.Template.FindName("DeButton", this); Button InButton = (Button)this.Template.FindName("InButton", this); //DeButton.MouseLeftButtonDown += DeButton_MouseLeftButtonDown; //InButton.MouseLeftButtonDown += InButton_MouseLeftButtonDown; DeButton.Click += DeButton_Click; InButton.Click += InButton_Click; } private void InButton_Click(object sender, RoutedEventArgs e) { if (!String.IsNullOrEmpty(this.Text)) { this.Text = int.Parse(this.Text) + Step + ""; this.SelectionStart = this.Text.Length; } else { this.Text = Minimum + ""; this.SelectionStart = this.Text.Length; } } private void DeButton_Click(object sender, RoutedEventArgs e) { if (!String.IsNullOrEmpty(this.Text)) { this.Text = int.Parse(this.Text) - Step < Minimum ? Minimum + "" : int.Parse(this.Text) - Step + ""; this.SelectionStart = this.Text.Length; } else { this.Text = Minimum + ""; this.SelectionStart = this.Text.Length; } } }
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ZyyszgSoftRobot.Controls"> <Style x:Key="IconFont"> <Setter Property="TextElement.FontFamily" Value="pack://application:,,,/ZyyszgSoftRobot;component/resource/#layui-icon"/> </Style> <Style x:Key="IconButton" TargetType="Button"> <Setter Property="Background" Value="White"></Setter> <Setter Property="BorderThickness" Value="0"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid> <!-- 背景和边框 --> <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="#CCCCCC" BorderThickness="1" CornerRadius="4"/> <!-- 图片区域(居中显示) --> <TextBlock x:Name="btnImage" Style="{StaticResource IconFont}" Width="18" Height="18" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{TemplateBinding Content}"/> </Grid> <!-- 交互状态触发器 --> <ControlTemplate.Triggers> <!-- 鼠标悬停效果 --> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" Value="#E0E0E0"/> </Trigger> <!-- 按下效果 --> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="border" Property="Background" Value="#D0D0D0"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="{x:Type local:NumberTextBox}"> <Setter Property="Minimum" Value="0"></Setter> <Setter Property="Text" Value="0"></Setter> <Setter Property="Cursor" Value="IBeam" /> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="Foreground" Value="Black" /> <Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:NumberTextBox}"> <Border Name="Border" Background="White" BorderBrush="#AAA" BorderThickness="1" CornerRadius="3"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="30" /> </Grid.ColumnDefinitions> <ScrollViewer x:Name="PART_ContentHost" Margin="6,0,0,0" VerticalAlignment="{TemplateBinding VerticalAlignment}" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" /> <TextBlock x:Name="Hint" Margin="6,0,0,0" VerticalAlignment="{Binding ElementName=PART_ContentHost, Path=VerticalAlignment}" Foreground="#c9ccd7" Text="{TemplateBinding Tag}" Visibility="Collapsed" /> <Border Grid.Column="1" Background="Transparent" BorderBrush="#eee" BorderThickness="1,0,0,0"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="Auto" /> <RowDefinition /> </Grid.RowDefinitions> <Button x:Name="InButton" VerticalAlignment="Center" Cursor="Hand" Foreground="#c2c2c2" Style="{StaticResource IconButton}" Content="" /> <Button x:Name="DeButton" Grid.Row="2" VerticalAlignment="Center" Cursor="Hand" Foreground="#c2c2c2" Content="" Style="{StaticResource IconButton}" /> </Grid> </Border> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="Text" Value="{x:Null}"> <Setter TargetName="Hint" Property="Visibility" Value="{x:Static Visibility.Visible}" /> </Trigger> <Trigger Property="Text" Value=""> <Setter TargetName="Hint" Property="Visibility" Value="{x:Static Visibility.Visible}" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
其中button的样式使用了iconfont,如果不想用可以改为图片或者 Path
哦对了,这个也是我copy的 忘了copy的谁的了,在原来的代码上稍微做了修改
不想平凡,奈何太懒 T_T

浙公网安备 33010602011771号