数据库的长度是根据字节来计量的

前两日做二个种类的时候,由于页面未有约束TextBox的输入长度,所以,后台平素报错了,超过数据库最大的长度。

数据库的长短是安分守纪字节来总括的,何况不一致的编码格式,汉字占用的字节长度又不相似,比如,大家用的是UTF8,三个汉字是3个字节,而默许的Default,三个中黄炎子孙民共和国字是2个字节。

TextBox有个马克斯Length属性,可是那性情情是不太符合供给的,因为这么些尺寸,是限量了输入的长短,比如设置20,则无论数字、字母、汉字最大的尺寸都以十多少个,但是,对于数据库来讲,长度却不平等了,

就此,不能够使用这些性格。

为了,统黄金年代清除下那些标题,所以给TextBox写了附加属性。

 

生机勃勃、想要的效果

用了附加属性,想达到一个怎么样意义啊,正是像设置马克斯Length相通,大器晚成旦到了数据库的字节长度,就不再能输入了。

据此,最开端想找四个限量输入的质量,缺憾作者学的太浅薄,未有找到相关的习性,由此,最后在同事的唤起下,能够记录上叁回的剧情,然后,假设超长,就用上贰次的从头到尾的经过张开赋值

图片 1

图片 2

二、附加属性

既然要用附加属性,况兼方便使用,那自然要给开采者暴表露来起码多个:马克斯ByteLength用来安装最大的字节数,EncodeModel用来设置编码格式

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

图片 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;
        }
    }

 

 使用格局如下:

图片 4

马克斯ByteLength是必须设置的从未有过进展默许,EncodeModel能够不安装不过由于是咱们和煦用,所以默许是UTF8,能够自动修正代码,依据你们公司的编码格式,那样也就不要赋值了。

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

相关文章