WPF TextBox按字节长度限制输入。WPF TextBox按字节长度限制输入。

前面少天举行一个档之下,由于页面没有限定TextBox的输入长度,所以,后台一直报错了,超出数据库最可怜的长。

前面少上举行一个色之上,由于页面没有范围TextBox的输入长度,所以,后台一直报错了,超出数据库最酷的尺寸。

数据库的长短是据字节来计算的,而且不同之编码格式,汉字占用的字节长度又非相同,比如,我们用底是UTF8,一个汉字是3个字节,而默认的Default,一个字是2只字节。

数据库的长是随字节来测算的,而且不同的编码格式,汉字占用的字节长度又非平等,比如,我们因此之是UTF8,一个字是3单字节,而默认的Default,一个汉字是2独字节。

TextBox有个MaxLength属性,但是这特性是未太相符要求的,因为是尺寸,是限量了输入的长度,比如设置20,则不管数字、字母、汉字最酷之尺寸还是20独,但是,对于数据库来说,长度也休一致了,

TextBox有只MaxLength属性,但是是特性是休极端符合要求的,因为这个长度,是限量了输入的长,比如设置20,则不管数字、字母、汉字最要命之长短都是20单,但是,对于数据库来说,长度也休同等了,

为此,不能够运用此特性。

就此,不克用这个特性。

以,统一解决下之题目,所以让TextBox写了增大属性。

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

 

 

一致、想使之效用

同等、想只要的功用

于是了附加属性,想达到一个呀效果啊,就是比如说设置MaxLength一样,一旦到了数据库的字节长度,就不再能够输入了。

因此了增大属性,想达到一个什么效力啊,就是如设置MaxLength一样,一旦到了数据库的字节长度,就不再能够输入了。

于是,最开始思念寻找一个限制输入的习性,可惜我学的极度浅薄,没有找到有关的属性,因此,最后在同事的提拔下,可以记下及一样涂鸦的始末,然后,如果超长,就因此上一致不成的情节开展赋值

故,最开头想念搜寻一个克输入的性,可惜我套的极浅薄,没有找到相关的习性,因此,最后当同事的提拔下,可以记录上等同糟的情节,然后,如果超长,就就此上平等赖的始末展开赋值

图片 1

图片 2

图片 3

图片 4

老二、附加属性

第二、附加属性

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

既然要因此附加属性,并且方便使用,那必然使叫开发者暴露出来至少少独:MaxByteLength用来安极端要命之字节数,EncodeModel用来装编码格式

EncodeModel是故Menu类型来开的,方便使用时直敲内容

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

图片 5

图片 6

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

自然上面是直接想用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;
        }
    }
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;
        }
    }

 

 

 使用方式如下:

 使用方法如下:

图片 7

图片 8

MaxByteLength是须安装的无展开默认,EncodeModel可以无设置但是由是咱们友好之所以,所以默认是UTF8,可以自动修改代码,按照你们公司之编码格式,这样呢便无须赋值了。

MaxByteLength是得设置的莫展开默认,EncodeModel可以免装但是由于是我们协调因此,所以默认是UTF8,可以活动修改代码,按照你们企业的编码格式,这样啊就算毫无赋值了。

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

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

相关文章

admin

网站地图xml地图