房地产公司WPF TextBox按字节长度限制输入

前面片上举行一个色的时刻,由于页面没有限定TextBox的输入长度,所以,后台一直报错了,超出数据库最充分之长。

数据库的长是按部就班字节来算的,而且不同之编码格式,汉字占用的字节长度又休雷同,比如,我们之所以的凡UTF8,一个中国字是3个字节,而默认的Default,一个汉字是2只字节。

TextBox有只MaxLength属性,但是是特性是免极端符合要求的,因为这个尺寸,是限量了输入的尺寸,比如安20,则凭数字、字母、汉字最老之长都是20独,但是,对于数据库来说,长度也未一致了,

故此,不克利用此特性。

为了,统一解决下之题目,所以受TextBox写了附加属性。

 

平、想如果的作用

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

就此,最开始想念搜寻一个克输入的特性,可惜我套的最浅薄,没有找到有关的性质,因此,最后当同事的唤起下,可以记下及等同不良的情,然后,如果超长,就因此上同样糟糕的情展开赋值

房地产公司 1

房地产公司 2

亚、附加属性

既要为此附加属性,并且方便使用,那必将使受开发者暴露出至少少单:MaxByteLength用来设置极端老之字节数,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

MaxByteLength是得设置的莫进展默认,EncodeModel可以免安装但是由于是我们温馨用,所以默认是UTF8,可以活动修改代码,按照你们企业之编码格式,这样啊就算绝不赋值了。

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

相关文章