溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

WPF中TextBox如何實現按字節(jié)長度限制輸入功能

發(fā)布時間:2021-07-10 11:14:09 來源:億速云 閱讀:212 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關WPF中TextBox如何實現按字節(jié)長度限制輸入功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

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

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

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

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

一、想要的效果

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

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

WPF中TextBox如何實現按字節(jié)長度限制輸入功能

WPF中TextBox如何實現按字節(jié)長度限制輸入功能

二、附加屬性

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

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

WPF中TextBox如何實現按字節(jié)長度限制輸入功能

本來上面是直接想用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如何實現按字節(jié)長度限制輸入功能

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

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

關于“WPF中TextBox如何實現按字節(jié)長度限制輸入功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI