国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

WPF TextBox實現按字節長度限制輸入功能

2019-10-29 21:09:01
字體:
來源:轉載
供稿:網友

前兩天做一個項目的時候,由于頁面沒有限制TextBox的輸入長度,所以,后臺直接報錯了,超出數據庫最大的長度。

數據庫的長度是按照字節來計算的,而且不同的編碼格式,漢字占用的字節長度又不相同,比如,我們用的是UTF8,一個漢字是3個字節,而默認的Default,一個漢字是2個字節。

TextBox有個MaxLength屬性,但是這個屬性是不太合乎要求的,因為這個長度,是限制了輸入的長度,比如設置20,則無論是數字、字母、漢字最大的長度都是20個,但是,對于數據庫來說,長度卻不相同了,所以,不能使用這個屬性。

為了,統一解決下這個問題,所以給TextBox寫了附加屬性。

一、想要的效果

用了附加屬性,想達到一個什么效果呢,就是像設置MaxLength一樣,一旦到了數據庫的字節長度,就不再能輸入了。

因此,最開始想找一個限制輸入的屬性,可惜我學的太淺薄,沒有找到相關的屬性,因此,最后在同事的提醒下,可以記錄上一次的內容,然后,如果超長,就用上一次的內容進行賦值

WPF,TextBox,字節長度

WPF,TextBox,字節長度

二、附加屬性

既然要用附加屬性,并且方便使用,那肯定要給開發者暴露出來至少兩個:MaxByteLength用來設置最大的字節數,EncodeModel用來設置編碼格式

EncodeModel是用Menu類型來做的,方便使用時直接敲內容

WPF,TextBox,字節長度

本來上面是直接想用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.TextChanged += Tb_TextChanged; } private static void Tb_TextChanged(object sender, TextChangedEventArgs 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; } }

使用方法如下:

WPF,TextBox,字節長度

MaxByteLength是必須設置的沒有進行默認,EncodeModel可以不設置但是由于是我們自己用,所以默認是UTF8,可以自行修改代碼,按照你們公司的編碼格式,這樣也就不用賦值了。

 代碼已修正,感謝Presia發現的BUG,疏忽了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 衡山县| 汾阳市| 庐江县| 德格县| 来凤县| 明光市| 平原县| 柳江县| 蓬莱市| 山阴县| 海门市| 长乐市| 泽库县| 宝丰县| 洛南县| 博乐市| 泽普县| 佛山市| 诏安县| 湖南省| 石泉县| 临猗县| 桓台县| 苍梧县| 乌兰浩特市| 固阳县| 宁阳县| 兴仁县| 大理市| 府谷县| 鲁甸县| 喀什市| 榆社县| 南川市| 盐城市| 紫阳县| 蒲城县| 香港| 襄樊市| 台中县| 太仆寺旗|