WPF TextBox實(shí)現(xiàn)按字節(jié)長度限制輸入功能
前兩天做一個(gè)項(xiàng)目的時(shí)候,由于頁面沒有限制TextBox的輸入長度,所以,后臺(tái)直接報(bào)錯(cuò)了,超出數(shù)據(jù)庫最大的長度。
數(shù)據(jù)庫的長度是按照字節(jié)來計(jì)算的,而且不同的編碼格式,漢字占用的字節(jié)長度又不相同,比如,我們用的是UTF8,一個(gè)漢字是3個(gè)字節(jié),而默認(rèn)的Default,一個(gè)漢字是2個(gè)字節(jié)。
TextBox有個(gè)MaxLength屬性,但是這個(gè)屬性是不太合乎要求的,因?yàn)檫@個(gè)長度,是限制了輸入的長度,比如設(shè)置20,則無論是數(shù)字、字母、漢字最大的長度都是20個(gè),但是,對(duì)于數(shù)據(jù)庫來說,長度卻不相同了,所以,不能使用這個(gè)屬性。
為了,統(tǒng)一解決下這個(gè)問題,所以給TextBox寫了附加屬性。
一、想要的效果
用了附加屬性,想達(dá)到一個(gè)什么效果呢,就是像設(shè)置MaxLength一樣,一旦到了數(shù)據(jù)庫的字節(jié)長度,就不再能輸入了。
因此,最開始想找一個(gè)限制輸入的屬性,可惜我學(xué)的太淺薄,沒有找到相關(guān)的屬性,因此,最后在同事的提醒下,可以記錄上一次的內(nèi)容,然后,如果超長,就用上一次的內(nèi)容進(jìn)行賦值
二、附加屬性
既然要用附加屬性,并且方便使用,那肯定要給開發(fā)者暴露出來至少兩個(gè):MaxByteLength用來設(shè)置最大的字節(jié)數(shù),EncodeModel用來設(shè)置編碼格式
EncodeModel是用Menu類型來做的,方便使用時(shí)直接敲內(nèi)容
本來上面是直接想用Encoding來做的,奈何它是抽象類,只好,寫個(gè)方法進(jìn)行了一部轉(zhuǎn)化,并且把Encoding類型的屬性進(jìn)行private。
大致上也就是這么一個(gè)思路,下面上代碼,給需要的人使用。
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; } }
使用方法如下:
MaxByteLength是必須設(shè)置的沒有進(jìn)行默認(rèn),EncodeModel可以不設(shè)置但是由于是我們自己用,所以默認(rèn)是UTF8,可以自行修改代碼,按照你們公司的編碼格式,這樣也就不用賦值了。
代碼已修正,感謝Presia發(fā)現(xiàn)的BUG,疏忽了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:判斷一個(gè)整數(shù)是否是2的N次冪實(shí)現(xiàn)方法
本文標(biāo)題:WPF TextBox實(shí)現(xiàn)按字節(jié)長度限制輸入功能
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5403.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery