数据库的尺寸是遵从字节来测算的

前二日做一个类型的时候,由于页面未有界定TextBox的输入长度,所以,后台平素报错了,高出数据库最大的长短。

数据库的尺寸是坚守字节来计量的,并且分裂的编码格式,汉字占用的字节长度又不等同,比方,大家用的是UTF8,两个中中原人民共和国字是3个字节,而暗中认可的Default,三个中国字是2个字节。

TextBox有个MaxLength属性,不过这么些天性是不太符合须求的,因为这么些长度,是限量了输入的长度,比如设置20,则无论数字、字母、汉字最大的长短都以21个,但是,对于数据库来讲,长度却不一致样了,

进而,不可能采纳那个天性。

为了,统一消除下那个题目,所以给TextBox写了增大属性。

 

一、想要的功能

用了增大属性,想到达一个怎样坚守呢,便是像设置马克斯Length同样,一旦到了数据库的字节长度,就不再能输入了。

所以,最开头想找三个限制输入的质量,缺憾小编学的太浅薄,未有找到相关的性质,由此,最终在同事的唤起下,能够记录上贰次的内容,然后,假使超长,就用上一遍的从头到尾的经过展开赋值

公海赌船710 1

公海赌船710 2

公海赌船710,二、附加属性

既然如此要用附加属性,而且方便使用,那必将要给开拓者暴表露来最少八个:马克斯ByteLength用来设置最大的字节数,EncodeModel用来安装编码格式

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

公海赌船710 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;
        }
    }

 

 使用情势如下:

公海赌船710 4

马克斯ByteLength是必需设置的从未有过开展暗中同意,EncodeModel可以不安装然则出于是我们团结用,所以默许是UTF8,可以自动修改代码,依据你们公司的编码格式,那样也就不要赋值了。

 代码已校勘,谢谢Presia发现的BUG,疏忽了

相关文章