码迷,mamicode.com
首页 > Windows程序 > 详细

[.NET] 在WPF中实现TextBox只能输入数字

时间:2015-09-02 07:03:01      阅读:712      评论:0      收藏:0      [点我收藏+]

标签:

如题,实现此功能的方法有很多,如果使用的是MVVM的开发模式,该如何实现此功能? 很简单,为TextBox新建一个附加属性,就可以完美的解决此问题。

新建一个附件属性 IsOnlyAcceptFigure ,并为其添加属性回调函数即可。

代码如下:

namespace WPF.Validation
{
    public class TextBoxOnlyAcceptFigureBehavior
    {
        public static readonly DependencyProperty IsOnlyAcceptFigureProperty =
            DependencyProperty.RegisterAttached("IsOnlyAcceptFigure", typeof (bool),
                typeof (TextBoxOnlyAcceptFigureBehavior),
                new UIPropertyMetadata(false, OnBindedPasswordChanged));

        public static bool GetIsOnlyAcceptFigure(DependencyObject obj)
        {
            return (bool) obj.GetValue(IsOnlyAcceptFigureProperty);
        }

        public static void SetIsOnlyAcceptFigure(DependencyObject obj, bool value)
        {
            obj.SetValue(IsOnlyAcceptFigureProperty, value);
        }

        private static void OnBindedPasswordChanged(DependencyObject obj,
            DependencyPropertyChangedEventArgs e)
        {
            var textBox = obj as TextBox;

            if (textBox == null)
                return;

            if ((bool) e.NewValue)
            {
                textBox.PreviewTextInput += textBox_PreviewTextInput;
                textBox.PreviewKeyDown += textBox_PreviewKeyDown;
                DataObject.AddPastingHandler(textBox, textBox_Pasting);
            }
            else
            {
                textBox.PreviewTextInput -= textBox_PreviewTextInput;
                textBox.PreviewKeyDown -= textBox_PreviewKeyDown;
                DataObject.RemovePastingHandler(textBox, textBox_Pasting);
            }
        }

        private static void textBox_Pasting(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof (string)))
            {
                var text = (string) e.DataObject.GetData(typeof (string));
                if (!IsNumberic(text))
                {
                    e.CancelCommand();
                }
            }
            else
            {
                e.CancelCommand();
            }
        }

        private static void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
                e.Handled = true;
        }

        private static void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !IsNumberic(e.Text);
        }

        public static bool IsNumberic(string _string)
        {
            if (string.IsNullOrEmpty(_string))
                return false;
            return _string.All(char.IsDigit);
        }
    }
}

如何调用:

<TextBox local:TextBoxOnlyAcceptFigureBehavior.IsOnlyAcceptFigure="True"/>

PS:验证输入的代码来自于网络,再次谢谢网友的分享。

祝大家生活愉快!

 

[.NET] 在WPF中实现TextBox只能输入数字

标签:

原文地址:http://www.cnblogs.com/sela/p/4777506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!