亚洲必赢bwin696.comWPF TextBox按字节长度限制输入

面前少龙举行一个项目之时光,由于页面没有限制TextBox的输入长度,所以,后台一直报错了,超出数据库最充分的尺寸。

数据库的长是据字节来计量的,而且不同的编码格式,汉字占用的字节长度又休等同,比如,我们所以底是UTF8,一个汉字是3只字节,而默认的Default,一个中国字是2个字节。

TextBox有只MaxLength属性,但是是特性是不极端符合要求的,因为这个长度,是限制了输入的长,比如安20,则凭数字、字母、汉字最老之长短都是20个,但是,对于数据库来说,长度也无雷同了,

之所以,不克以此特性。

为了,统一解决下这个题材,所以让TextBox写了增大属性。

 

同、想使的效能

故此了附加属性,想上一个啊作用呢,就是像设置MaxLength一样,一旦到了数据库的字节长度,就不再会输入了。

之所以,最初步想寻找一个范围输入的性质,可惜我套的卓绝浅薄,没有找到相关的特性,因此,最后在同事的唤醒下,可以记录上一致潮的情节,然后,如果超长,就就此上平等不良的情开展赋值

亚洲必赢bwin696.com 1

亚洲必赢bwin696.com 2

次、附加属性

既是要用附加属性,并且方便使用,那肯定使为开发者暴露出来至少少单:MaxByteLength用来安装极端深之字节数,EncodeModel用来设置编码格式

EncodeModel是用Menu类型来举行的,方便使用时直接敲内容

亚洲必赢bwin696.com 3

自上面是直接想用Encoding来开的,奈何它是抽象类,只好,写单办法进行了一如既往部转化,并且将Encoding类型的性能进行private。

 

约莫上也尽管是这样一个思路,下面上代码,给要之人头以。 

public class MaxByteAttachedProperty : DependencyObject
    {
        public enum Encode
        {
            Default,
            ASCII,
            UTF8,
            UTF32,
            UTF7,
            BigEndianUnicode,
            Unicode
        }


        private static string GetPreText(DependencyObject obj)
        {
            return (string)obj.GetValue(PreTextProperty);
        }

        private static void SetPreText(DependencyObject obj, string value)
        {
            obj.SetValue(PreTextProperty, value);
        }

        // Using a DependencyProperty as the backing store for PreText.  This enables animation, styling, binding, etc...
        private static readonly DependencyProperty PreTextProperty =
            DependencyProperty.RegisterAttached("PreText", typeof(string), typeof(MaxByteAttachedProperty), new PropertyMetadata(""));


        public static int GetMaxByteLength(DependencyObject obj)
        {
            return (int)obj.GetValue(MaxByteLengthProperty);
        }

        public static void SetMaxByteLength(DependencyObject obj, int value)
        {
            obj.SetValue(MaxByteLengthProperty, value);
        }

        // Using a DependencyProperty as the backing store for MaxByteLength.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MaxByteLengthProperty =
            DependencyProperty.RegisterAttached("MaxByteLength", typeof(int), typeof(MaxByteAttachedProperty), new PropertyMetadata(OnTextBoxPropertyChanged));

        private static void OnTextBoxPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextBox tb = d as TextBox;
            if (tb == null)
            {
                return;
            }
            tb.PreviewTextInput += Tb_PreviewTextInput;
        }
        private static void Tb_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (IsOutMaxByteLength(tb.Text, tb))
            {
                tb.Text = GetPreText(tb);
                tb.Select(tb.Text.Length, 0);
                return;
            }
        }
        public static Encode GetEncodeModel(DependencyObject obj)
        {
            return (Encode)obj.GetValue(EncodeModelProperty);
        }

        public static void SetEncodeModel(DependencyObject obj, Encode value)
        {
            obj.SetValue(EncodeModelProperty, value);
        }

        // Using a DependencyProperty as the backing store for EncodeM.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EncodeModelProperty =
            DependencyProperty.RegisterAttached("EncodeModel", typeof(Encode), typeof(MaxByteAttachedProperty), new PropertyMetadata(Encode.UTF8, OnEncodeModelChanged));
        private static void OnEncodeModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SetEM(d, GetEncodeModel(d));
        }

        private static Encoding GetEncodingModel(DependencyObject obj)
        {
            return (Encoding)obj.GetValue(EncodingModelProperty);
        }

        private static void SetEncodingModel(DependencyObject obj, Encoding value)
        {
            obj.SetValue(EncodingModelProperty, value);
        }

        // Using a DependencyProperty as the backing store for EncodingModel.  This enables animation, styling, binding, etc...
        private static readonly DependencyProperty EncodingModelProperty =
            DependencyProperty.RegisterAttached("EncodingModel", typeof(Encoding), typeof(MaxByteAttachedProperty), new PropertyMetadata(Encoding.UTF8));

        private static void SetEM(DependencyObject obj, Encode e)
        {
            switch (e)
            {
                case Encode.Default:
                    SetEncodingModel(obj, Encoding.Default);
                    break;
                case Encode.ASCII:
                    SetEncodingModel(obj, Encoding.ASCII);
                    break;
                case Encode.UTF8:
                    SetEncodingModel(obj, Encoding.UTF8);
                    break;
                case Encode.UTF32:
                    SetEncodingModel(obj, Encoding.UTF32);
                    break;
                case Encode.UTF7:
                    SetEncodingModel(obj, Encoding.UTF7);
                    break;
                case Encode.BigEndianUnicode:
                    SetEncodingModel(obj, Encoding.BigEndianUnicode);
                    break;
                case Encode.Unicode:
                    SetEncodingModel(obj, Encoding.Unicode);
                    break;
                default:
                    break;
            }
        }

        private static bool IsOutMaxByteLength(string txt, DependencyObject obj)
        {
            int txtLength = GetEncodingModel(obj).GetBytes(txt).Length;//文本长度
            if (GetMaxByteLength(obj) >= txtLength)
            {
                SetPreText(obj, txt);
                return false;
            }
            return true;
        }
    }

 

 使用方式如下:

亚洲必赢bwin696.com 4

MaxByteLength是须装的没有进展默认,EncodeModel可以不安装但是由于是咱团结一心用,所以默认是UTF8,可以自行修改代码,按照你们店之编码格式,这样为便绝不赋值了。

 代码已更正,感谢Presia发现的BUG,疏忽了

相关文章