由于项目需要,最近写了一个数字输入文本框,在此作个备忘。

1.代码调用

<controls:NumericBox Height="32" Width="80"
                                             MinValue ="0"
                                             MaxValue="100"
                                             Digits ="0"
                                             CurValue="{Binding LabelSize, Mode=TwoWay}" >
                                </controls:NumericBox>

2.样式(InputMethod.IsInputMethodEnabled项设置为false后可以屏蔽输入法)

<Style TargetType="{x:Type local:NumericBox}"  BasedOn="{StaticResource MetroTextBox}">
        <Setter Property="InputMethod.IsInputMethodEnabled" Value="False" />
 </Style>

3.后台逻辑

public class NumericBox : TextBox
    {
        #region DependencyProperty
        private const double CURVALUE = 0; //当前值
        private const double MINVALUE = double.MinValue; //最小值
        private const double MAXVALUE = double.MaxValue; //最大值
        private const int DIGITS = 15; //小数点精度

public static readonly DependencyProperty CurValueProperty;
        public static readonly DependencyProperty MinValueProperty;
        public static readonly DependencyProperty MaxValueProperty;
        public static readonly DependencyProperty DigitsProperty;

public double CurValue
        {
            get
            {
                return (double)GetValue(CurValueProperty);
            }
            set
            {
                double v = value;
                if (value < MinValue)
                {
                    v = MinValue;
                }
                else if (value > MaxValue)
                {
                    v = MaxValue;
                }
                v = Math.Round(v, Digits);

SetValue(CurValueProperty, v);
                // if do not go into OnCurValueChanged then force update ui
                if (v != value)
                {
                    this.Text = v.ToString();
                }
            }
        }
        public double MinValue
        {
            get
            {
                return (double)GetValue(MinValueProperty);
            }
            set
            {
                SetValue(MinValueProperty, value);
            }
        }
        public double MaxValue
        {
            get
            {
                return (double)GetValue(MaxValueProperty);
            }
            set
            {
                SetValue(MaxValueProperty, value);
            }
        }
        public int Digits
        {
            get
            {
                return (int)GetValue(DigitsProperty);
            }
            set
            {
                int digits = value;
                if (digits <= 0)
                {
                    digits = 0;
                }
                if (digits > 15)
                {
                    digits = 15;
                }
                SetValue(DigitsProperty, value);
            }
        }

static NumericBox()
        {
            FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(CURVALUE, new PropertyChangedCallback(OnCurValueChanged));
            CurValueProperty = DependencyProperty.Register("CurValue", typeof(double), typeof(NumericBox), metadata);

metadata = new FrameworkPropertyMetadata(MINVALUE, new PropertyChangedCallback(OnMinValueChanged));
            MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(NumericBox), metadata);

metadata = new FrameworkPropertyMetadata(MAXVALUE, new PropertyChangedCallback(OnMaxValueChanged));
            MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(NumericBox), metadata);

metadata = new FrameworkPropertyMetadata(DIGITS, new PropertyChangedCallback(OnDigitsChanged));
            DigitsProperty = DependencyProperty.Register("Digits", typeof(int), typeof(NumericBox), metadata);
        }

private static void OnCurValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            double value = (double)e.NewValue;
            NumericBox numericBox = (NumericBox)sender;
            numericBox.Text = value.ToString();
        }
        private static void OnMinValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            double minValue = (double)e.NewValue;
            NumericBox numericBox = (NumericBox)sender;
            numericBox.MinValue = minValue;
        }
        private static void OnMaxValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            double maxValue = (double)e.NewValue;
            NumericBox numericBox = (NumericBox)sender;
            numericBox.MaxValue = maxValue;
        }
        private static void OnDigitsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            int digits = (int)e.NewValue;
            NumericBox numericBox = (NumericBox)sender;
            numericBox.CurValue = Math.Round(numericBox.CurValue, digits);
            numericBox.MinValue = Math.Round(numericBox.MinValue, digits);
            numericBox.MaxValue = Math.Round(numericBox.MaxValue, digits);
        }
        #endregion

public NumericBox()
        {
            this.TextChanged += NumericBox_TextChanged;
            this.PreviewKeyDown += NumericBox_KeyDown;
            this.LostFocus += NumericBox_LostFocus;
            DataObject.AddPastingHandler(this, NumericBox_Pasting);
        }

void NumericBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            NumericBox numericBox = sender as NumericBox;
            if (string.IsNullOrEmpty(numericBox.Text))
            {
                return;
            }

TrimZeroStart();

double value = MinValue;
            if (!Double.TryParse(numericBox.Text, out value))
            {
                return;
            }

if (value != this.CurValue)
            {
                this.CurValue = value;
            }
        }

void NumericBox_KeyDown(object sender, KeyEventArgs e)
        {
            Key key = e.Key;
            if(IsControlKeys(key))
            {
                return;
            }
            else if (IsDigit(key))
            {
                return;
            }
            else if (IsSubtract(key)) //-
            {
                TextBox textBox = sender as TextBox;
                string str = textBox.Text;
                if (str.Length > 0 && textBox.SelectionStart != 0)
                {
                    e.Handled = true;
                }
            }
            else if (IsDot(key)) //point
            {
                if (this.Digits > 0)
                {
                    TextBox textBox = sender as TextBox;
                    string str = textBox.Text;
                    if (str.Contains('.') || str == "-")
                    {
                        e.Handled = true;
                    }
                }
                else
                {
                    e.Handled = true;
                }
            }
            else
            {
                e.Handled = true;
            }
        }

void NumericBox_LostFocus(object sender, RoutedEventArgs e)
        {
            NumericBox numericBox = sender as NumericBox;
            if (string.IsNullOrEmpty(numericBox.Text))
            {
                numericBox.Text = this.CurValue.ToString();
            }
        }

private void NumericBox_Pasting(object sender, DataObjectPastingEventArgs e)
        {
            e.CancelCommand();
        }

private static readonly List<Key> _controlKeys = new List<Key>
                                                             {
                                                                 Key.Back,
                                                                 Key.CapsLock,
                                                                 Key.Down,
                                                                 Key.End,
                                                                 Key.Enter,
                                                                 Key.Escape,
                                                                 Key.Home,
                                                                 Key.Insert,
                                                                 Key.Left,
                                                                 Key.PageDown,
                                                                 Key.PageUp,
                                                                 Key.Right,
                                                                 Key.Tab,
                                                                 Key.Up
                                                             };
        public static bool IsControlKeys(Key key)
        {
            return _controlKeys.Contains(key);
        }

public static bool IsDigit(Key key)
        {
            bool shiftKey = (Keyboard.Modifiers & ModifierKeys.Shift) != 0;
            bool retVal;
            if (key >= Key.D0 && key <= Key.D9 && !shiftKey)
            {
                retVal = true;
            }
            else
            {
                retVal = key >= Key.NumPad0 && key <= Key.NumPad9;
            }
            return retVal;
        }

public static bool IsDot(Key key)
        {
            bool shiftKey = (Keyboard.Modifiers & ModifierKeys.Shift) != 0;
            bool flag = false;
            if (key == Key.Decimal)
            {
                flag = true;
            }
            if (key == Key.OemPeriod && !shiftKey)
            {
                flag = true;
            }
            return flag;
        }
        public static bool IsSubtract(Key key)
        {
            bool shiftKey = (Keyboard.Modifiers & ModifierKeys.Shift) != 0;
            bool flag = false;
            if (key == Key.Subtract)
            {
                flag = true;
            }
            if (key == Key.OemMinus && !shiftKey)
            {
                flag = true;
            }
            return flag;
        }

private void TrimZeroStart()
        {
            if(this.Text.Length == 1)
            {
                return;
            }
            string resultText = this.Text;
            int zeroCount = 0;
            foreach (char c in this.Text)
            {
                if (c == '0') { zeroCount++; }
                else { break; }
            }
            if (zeroCount == 0)
            {
                return;
            }

if (this.Text.Contains('.'))
            {
                if (this.Text[zeroCount] != '.')
                {
                    resultText = this.Text.TrimStart('0');
                }
                else if (zeroCount > 1)
                {
                    resultText = this.Text.Substring(zeroCount - 1);
                }
            }
            else if (zeroCount > 0)
            {
                resultText = this.Text.TrimStart('0');
            }
        }

}

WPF 自定义数字文本框:NumericBox的更多相关文章

  1. C#Winform使用扩展方法自定义富文本框(RichTextBox)字体颜色

    在利用C#开发Winform应用程序的时候,我们有可能使用RichTextBox来实现实时显示应用程序日志的功能,日志又分为:一般消息,警告提示 和错误等类别.为了更好地区分不同类型的日志,我们需要使 ...

  2. C# 全选中数字文本框内容

    /// <summary>        /// 全选中数字文本框内容        /// </summary>        /// <param name=&quo ...

  3. [WPF]实现TextBox文本框单击全选

    原文:[WPF]实现TextBox文本框单击全选 /// <summary>         /// Void:设置获取焦点时全选文本         /// </summary&g ...

  4. WPF自定义数字输入框控件

    要求:只能输入数字和小数点,可以设置最大值,最小值,小数点前长度,小数点后长度(支持绑定设置): 代码如下: using System; using System.Collections.Generi ...

  5. [C# WPF] 关于将文本框竖起来(旋转文字)

    xx.xmal.cs 后台代码中动态添加控件到 UI 文字显示在一个 Canvas 中(定位用Canvas.SetLeft() / Canvas.SetTop() ), 为了实现排版效果,可适当在 T ...

  6. WPF 自定义文本框输入法 IME 跟随光标

    本文告诉大家在 WPF 写一个自定义的文本框,如何实现让输入法跟随光标 本文非小白向,本文适合想开发自定义的文本框,从底层开始开发的文本库的伙伴.在开始之前,期望了解了文本库开发的基础知识 本文实现的 ...

  7. [JS] 文本框判断输入的内容是否为数字

    可以通过触发文本框的onchange事件来对输入的内容进行判断是否为数字 文本框的属性设置: 把onchange的属性对应的js函数写好即可 参数传输的是当前控件的value值,即text值 < ...

  8. JS学习笔记 - 自定义右键菜单、文本框只能输入数字

    <script> // 事件总共有2个部分, //1.点击鼠标右键的表现 oncontextmenu 2.点击鼠标左键的表现(即普通点击onclick) // 点击右键,div位置定位到鼠 ...

  9. JavaScript 自定义文本框光标——初级版

    文本框(input或textarea)的光标无法修改样式(除了通过color修改光标颜色).但笔者希望个人创建自己的网站时,文本框的光标有属于自己的风格.所以,尝试模拟文本框的光标,设计有自己风格的光 ...

随机推荐

  1. 安卓MonkeyRunner源码分析之与Android设备通讯方式

    如前文<谁动了我的截图?--Monkeyrunner takeSnapshot方法源码跟踪分析>所述,本文主要会尝试描述android的自动化测试框架MonkeyRunner究竟是如何和目 ...

  2. 打印Ibatis最后,SQL声明

    做项目时,满足这一需求.我们希望最终打印出在数据库运行SQL声明,这些都普遍遇到了一些一般性问题.我会去Appfuse,结果这次没有成功.它是有相关的配置,可是好像没实用.我也就没有深查下去.我想这种 ...

  3. C#中调用Outlook API 发起会议

    原文:C#中调用Outlook API 发起会议 在我上一篇博文中曾提到了 SharePoint 中调用传出电子邮件中的邮件服务器及地址发送邮件 但是,里面的方法只能用于发送普通电子邮件.如果要发起会 ...

  4. [译]Java 设计模式之装饰器

    (文章翻译自Java Design Pattern: Decorator – Decorate your girlfriend) 1.装饰模式的来历 让我们假设你在寻找一个女朋友.有来自像没美国中国日 ...

  5. 在OpenWrt上编写自己的硬件操作程序

    上一篇文章中有写到如何使用OPENWRT的SDK,这里继续,写怎么在上面开发自己的应用程序. 我欲在OpenWrt上编写一个软件,它能够去读取某个AD芯片的多通道采样值. 在看这篇文章之前请看这官方的 ...

  6. Apache JMeter--网站自动测试与性能测评

    Apache JMeter--网站自动测试与性能测评 2013-02-28 15:48:05 标签:Jmeter From:http://bdql.iteye.com/blog/291987 出于学习 ...

  7. 【转】Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9671609 记得在很早之前,我写了一篇关于Android滑动菜单的文章,其中有一个 ...

  8. DDD(领域驱动设计)应对具体业务场景,如何聚焦 Domain Model(领域模型)?

    DDD(领域驱动设计)应对具体业务场景,如何聚焦 Domain Model(领域模型)? 阅读目录: 问题根源是什么? <领域驱动设计-软件核心复杂性应对之道>分层概念 Repositor ...

  9. ThoughtWorks开发持续集成及部署利器:Go

    持续集成及部署利器:Go   Go是一款先进的持续集成和发布管理系统,由ThoughtWorks开发.(不要和Google的编程语言Go混淆了!)其前身为Cruise,是ThoughtWorks在做咨 ...

  10. Java多线程学习笔记——从Java JVM对多线程数据同步的一些理解

       我们知道在多线程编程中,我们很大的一部分内容是为了解决线程间的资源同步问题和线程间共同协作解决问题.线程间的同步,通俗我们理解为僧多粥少,在粥有限情况下,我们怎么去防止大家有秩序的喝到粥,不至于 ...