customTextbox

本文介绍了一种基于 WPF 的自定义文本框控件,该控件支持多种类型的数据输入验证,包括整数、小数等,并通过依赖属性实现了动画、样式绑定等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;

namespace MyWPFCustomControls
{
    public class CustomTextBox : TextBox
    {
        public CustomTextBox()
        {

        }



        static CustomTextBox()
        {

        }
        public CustomTextBoxType CustomTextBoxType
        {
            get { return (CustomTextBoxType)GetValue(CustomTextBoxTypeProperty); }
            set { SetValue(CustomTextBoxTypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CustomTextBoxType.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CustomTextBoxTypeProperty =
            DependencyProperty.Register("CustomTextBoxType", typeof(CustomTextBoxType), typeof(CustomTextBox), new UIPropertyMetadata(CustomTextBoxType.None, new PropertyChangedCallback(OnCustomTextBoxTypeChanged)));


        private static void OnCustomTextBoxTypeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var tb = obj as CustomTextBox;

        }


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }
        protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
        {
            base.OnPreviewTextInput(e);
        }
        protected override void OnTextChanged(TextChangedEventArgs e)
        {
            base.OnTextChanged(e);
            //屏蔽中文输入和非法字符粘贴输入

            TextChange[] change = new TextChange[e.Changes.Count];
            e.Changes.CopyTo(change, 0);

            int offset = change[0].Offset;
            if (change[0].AddedLength > 0)
            {
                switch (this.CustomTextBoxType)
                {
                    case CustomTextBoxType.None:
                        break;
                    case CustomTextBoxType.Integer:
                        NonIntergerHandle(change, offset);
                        break;
                    case CustomTextBoxType.Decimal:
                        NonDecimalHandle(change, offset);
                        break;
                    case CustomTextBoxType.Alphabet:
                        break;
                    case CustomTextBoxType.Alphanumeric:
                        break;
                    case CustomTextBoxType.Currency:
                        break;
                    default:
                        break;
                }

            }
        }

        private void NonDecimalHandle(TextChange[] change, int offset)
        {
            Decimal num = 0;
            if (!Decimal.TryParse(this.Text, out num))
            {
                this.Text = this.Text.Remove(offset, change[0].AddedLength);
                this.Select(offset, 0);
            }
        }

        private void NonIntergerHandle(TextChange[] change, int offset)
        {          
            long num = 0;
            if (!long.TryParse(this.Text, out num))
            {
                this.Text = this.Text.Remove(offset, change[0].AddedLength);
                this.Select(offset, 0);
            }
        }

        protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            base.OnPreviewKeyDown(e);
            switch (this.CustomTextBoxType)
            {
                case CustomTextBoxType.None:
                    break;
                case CustomTextBoxType.Integer:
                    IntegerOperation(e);
                    break;
                case CustomTextBoxType.Decimal:
                    DecimalOperation(e);
                    break;
                case CustomTextBoxType.Alphabet:
                    break;
                case CustomTextBoxType.Alphanumeric:
                    break;
                case CustomTextBoxType.Currency:

                    break;
                default:
                    break;
            }


        }

        private void DecimalOperation(System.Windows.Input.KeyEventArgs e)
        {
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
            {

                if (this.Text.Contains(".") && e.Key == Key.Decimal)
                {
                    e.Handled = true;
                    return;
                }
                e.Handled = false;
            }
            else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
            {
                if (this.Text.Contains(".") && e.Key == Key.OemPeriod)
                {
                    e.Handled = true;
                    return;
                }
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void IntegerOperation(System.Windows.Input.KeyEventArgs e)
        {           
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
            {
                e.Handled = false;
            }
            else if ((e.Key >= Key.D0 && e.Key <= Key.D9) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }



    public enum CustomTextBoxType
    {
        None,
        Integer,
        Decimal,
        Alphabet,
        Alphanumeric,
        Currency
    }
}
View Code

 

转载于:https://www.cnblogs.com/FaDeKongJian/p/3149185.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值