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地图