当前位置: 首页 > web >正文

wpf 自定义控件,只能输入小数点,并且能控制小数点位数

using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;namespace 项目名称
{public class DoubleTextBox : TextBox{public double? MinValue{get { return (double?)GetValue(MinValueProperty); }set { SetValue(MinValueProperty, value); }}public static readonly DependencyProperty MinValueProperty =DependencyProperty.Register("MinValue", typeof(double?), typeof(DoubleTextBox));public double MaxValue{get { return (double)GetValue(MaxValueProperty); }set { SetValue(MaxValueProperty, value); }}public static readonly DependencyProperty MaxValueProperty =DependencyProperty.Register("MaxValue", typeof(double), typeof(DoubleTextBox), new PropertyMetadata(double.MaxValue));public static bool GetIsEnabled(DependencyObject obj){return (bool)obj.GetValue(IsGotFocusProperty);}public static void SetIsEnabled(DependencyObject obj, bool value){obj.SetValue(IsGotFocusProperty, value);}public bool IsNeedLoseFucos{get { return (bool)GetValue(IsNeedLoseFucosvProperty); }set { SetValue(IsNeedLoseFucosvProperty, value); }}public static readonly DependencyProperty IsNeedLoseFucosvProperty =DependencyProperty.RegisterAttached("IsNeedLoseFucos", typeof(bool), typeof(DoubleTextBox), new PropertyMetadata(true));public bool IsGotFocus{get { return (bool)GetValue(IsGotFocusProperty); }set { SetValue(IsGotFocusProperty, value); }}public static readonly DependencyProperty IsGotFocusProperty =DependencyProperty.RegisterAttached("IsGotFocus", typeof(bool), typeof(DoubleTextBox), new PropertyMetadata(false));public string PreviousText{get { return (string)GetValue(PreviousTextProperty); }set { SetValue(PreviousTextProperty, value); }}public static readonly DependencyProperty PreviousTextProperty =DependencyProperty.Register("PreviousText", typeof(string), typeof(DoubleTextBox), new PropertyMetadata(string.Empty));public int DecimalCount{get { return (int)GetValue(DecimalCountProperty); }set { SetValue(DecimalCountProperty, value); }}public static readonly DependencyProperty DecimalCountProperty =DependencyProperty.Register("DecimalCount", typeof(int), typeof(DoubleTextBox), new PropertyMetadata(3));public string HistoryText{get { return (string)GetValue(HistoryTextProperty); }set { SetValue(HistoryTextProperty, value); }}public static readonly DependencyProperty HistoryTextProperty = DependencyProperty.Register("HistoryText", typeof(string), typeof(DoubleTextBox), new PropertyMetadata(string.Empty));static DoubleTextBox(){DefaultStyleKeyProperty.OverrideMetadata(typeof(DoubleTextBox), new FrameworkPropertyMetadata(typeof(DoubleTextBox)));}public DoubleTextBox(){LostFocus += DoubleTextBox_LostFocus;PreviewTextInput += DoubleTextBox_PreviewTextInput;}private void DoubleTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e){Text = Text.Trim();string txt = Text + e.Text;if (!double.TryParse(txt, out double value)){if (!string.IsNullOrWhiteSpace(Text)){var index = Text.IndexOf(e.Text);if (index > -1) { Text = Text.Remove(index); }SelectionStart = Text.Length + e.Text.Length;}e.Handled = true; // 阻止输入非数字字符}else if (value > MaxValue){Text = MaxValue.ToString(); // 将文本框的值设置为最大值SelectionStart = Text.Length + e.Text.Length;e.Handled = true; // 阻止输入超出取值范围的数字}else if (txt.Contains('.')){var Has = txt.Split('.');if (Has != null && Has.Length > 0 && Has[1] != null){if (Has[1].Length == DecimalCount){double dis = double.Parse(txt);if (MinValue is not null && dis < MinValue && txt.Length >= MinValue.Value.ToString().Length){Text = MinValue.Value.ToString(); // 将文本框的值设置为最小值SelectionStart = Text.Length + e.Text.Length;e.Handled = true; // 阻止输入非数字字符}}else if (Has[1].Length > DecimalCount){Text = string.Format("{0:f" + DecimalCount + "}", value);double dis = double.Parse(txt);if (MinValue is not null && dis < MinValue && txt.Length >= MinValue.Value.ToString().Length){Text = MinValue.Value.ToString(); // 将文本框的值设置为最小值}SelectionStart = Text.Length + e.Text.Length;e.Handled = true; // 阻止输入非数字字符}}}else{Text = value.ToString();SelectionStart = Text.Length + e.Text.Length;e.Handled = true;}SelectionStart = Text.Length + e.Text.Length;HistoryText = Text;}private void DoubleTextBox_LostFocus(object sender, RoutedEventArgs e){IsGotFocus = false;Text = Text.Trim();if (string.IsNullOrEmpty(Text)){if (!string.IsNullOrEmpty(HistoryText) && double.TryParse(HistoryText, out double history) && history > MinValue){Text = HistoryText;}else if(!string.IsNullOrEmpty(PreviousText)){Text = PreviousText;}else if (MinValue is not null){Text = MinValue.Value.ToString();}SelectionStart = Text.Length;}else{if (double.TryParse(Text, out double num)){if (num < MinValue){if (!string.IsNullOrEmpty(HistoryText) && double.TryParse(HistoryText, out double history) && history > MinValue){Text = HistoryText;}else{Text = MinValue.ToString();}}else if (num > MaxValue){if (!string.IsNullOrEmpty(HistoryText) && double.TryParse(HistoryText, out double history) && history < MaxValue){Text = HistoryText;}else if (!string.IsNullOrEmpty(PreviousText)){Text = PreviousText;}else{Text = MaxValue.ToString();}}}}}}
}

调用:DecimalCount就是小数点位数,一般不去设置,最多三位小数点。如果失去焦点,当前的数据会时你上次输入的数据,这样不会出现数据为空的情况。

 xmlns:local="clr-namespace:你的项目名"  xmlns:sys="clr-namespace:System;assembly=mscorlib"  <local:DoubleTextBox MinValue="0.001" MaxValue="1000000000" PreviousText="1" Text="{Binding Dilution,TargetNullValue={x:Static sys:String.Empty}}" Width="183" Height="60" MaxLength="10" />

http://www.xdnf.cn/news/19929.html

相关文章:

  • 微服务多级缓存:从问题到实战(小白也能看懂的亿级流量方案)
  • FastJson
  • 技术框架之脚手架实现
  • .vsdx文件转pdf、word、ppt等文件在线分享(免费版)
  • Linux的墙上时钟和单调时钟的区别
  • Flutter环境搭建全攻略之-Macos环境搭建
  • Android 中自定义控件实现 AppCompatSpinner 功能
  • 面试复习题-Flutter场景题
  • 数据结构:双向链表
  • 题解:UVA1589 象棋 Xiangqi
  • 基于 CC-Link IE FB 转 DeviceNet 技术的三菱 PLC 与发那科机器人在汽车涂装线的精准喷涂联动
  • Augmentcode免费额度AI开发WordPress商城实战
  • 【全面指南】Claude Code 从入门到精通:安装、配置、命令与高级技巧详解
  • 一个线程池的工作线程run函数的解析
  • Docker 学习笔记
  • 52DH Pro网址导航系统开源版
  • 泰酷辣!我的头像被「移乐AI头像」‘爆改’成顶流了!免费快来薅!
  • 【FastDDS】Layer DDS之Domain (01-overview)
  • 深度学习之第六课卷积神经网络 (CNN)如何保存和使用最优模型
  • 因果机器学习热度攀升,成顶会顶刊 “加分项”,想发论文就认准它!
  • 苍穹外卖项目实战(日记十四)-记录实战教程及问题的解决方法-(day3课后作业) 菜品停售启售功能
  • 机器视觉中为什么优先选择黑白相机?
  • 【Linux】为什么死循环卡不死 Linux?3 个核心逻辑看懂进程优先级与 CPU 调度密码
  • 性能测试-jmeter9-直连数据库
  • 苍穹外卖项目笔记day03
  • 从0 死磕全栈第3天:React Router (Vite + React + TS 版):构建小时站实战指南
  • 机器学习-逻辑回归
  • raspberry Pi 4B(树莓派4B)开启VNC服务 主机用VNC连接
  • 14、Docker构建后端镜像并运行
  • 关于SPI串口spidev接收数据不完整的问题