The first two days to do a project, because the page does not limit the input length of the textbox, so the background directly error, exceeding the maximum length of the database.
The length of the database is calculated in bytes, and different encoding formats, Chinese characters occupy the same length of bytes, for example, we use UTF8, a Chinese character is 3 bytes, and the default, a Chinese character is 2 bytes.
TextBox has a MaxLength property, but this property is not very desirable, because this length is limited to the length of the input, such as setting 20, whether the number, letter, the maximum length of Chinese characters are 20, but for the database, the length is not the same,
Therefore, you cannot use this property.
In order to solve this problem uniformly, the textbox is written with additional attributes.
First, the desired effect
Using the attached property, want to achieve what effect, is like setting maxlength, once to the database byte length, you can no longer input.
Therefore, the first to find a restricted input properties, unfortunately I learned too shallow, did not find the relevant properties, so the last time in a colleague's reminder, you can record the previous content, and then, if the long, the last content to be assigned
Second, additional properties
Given the additional properties and ease of use, it will definitely expose developers to at least two: Maxbytelength is used to set the maximum number of bytes, Encodemodel is used to set the encoding format
Encodemodel is made with the menu type and is easy to use when you tap content directly
Originally above is directly want to use encoding to do, but it is abstract class, had to write a method to do a conversion, and the encoding type of attributes to private.
In general, it is such a way of thinking, the following code, to the needs of people to use.
Public classMaxbyteattachedproperty:dependencyobject {Static stringPretext =""; Public enumEncode {Default, ASCII, UTF8, UTF32, UTF7, Bigendianunicode, Unicode} Public Static intgetmaxbytelength (DependencyObject obj) {return(int) obj. GetValue (Maxbytelengthproperty); } Public Static voidSetmaxbytelength (DependencyObject obj,intvalue) {obj. SetValue (maxbytelengthproperty, value); } //Using a DependencyProperty as the backing store for Maxbytelength. This enables animation, styling, binding, etc ... Public Static ReadOnlyDependencyProperty Maxbytelengthproperty =dependencyproperty.registerattached ("Maxbytelength",typeof(int),typeof(Maxbyteattachedproperty),NewPropertyMetadata (ontextboxpropertychanged)); Private Static voidontextboxpropertychanged (DependencyObject D, DependencyPropertyChangedEventArgs e) {TextBox TB = d asTextBox; if(tb==NULL) { return; } TB. TextChanged+=tb_textchanged; } Private Static voidTb_textchanged (Objectsender, Textchangedeventargs e) {TextBox TB= Sender asTextBox; if(Isoutmaxbytelength (TB. TEXT,TB)) {TB. Text=pretext; Tb. Select (TB. Text.length,0); return; } } Public StaticEncode Getencodemodel (DependencyObject obj) {return(Encode) obj. GetValue (Encodemodelproperty); } Public Static voidSetencodemodel (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 ReadOnlyDependencyProperty Encodemodelproperty =dependencyproperty.registerattached ("Encodemodel",typeof(Encode),typeof(Maxbyteattachedproperty),NewPropertyMetadata (Encode.utf8, onencodemodelchanged)); Private Static voidonencodemodelchanged (DependencyObject D, DependencyPropertyChangedEventArgs e) {Setem (D,getencodem Odel (d)); } Private StaticEncoding Getencodingmodel (DependencyObject obj) {return(Encoding) obj. GetValue (Encodingmodelproperty); } Private Static voidSetencodingmodel (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 ReadOnlyDependencyProperty Encodingmodelproperty =dependencyproperty.registerattached ("Encodingmodel",typeof(Encoding),typeof(Maxbyteattachedproperty),NewPropertyMetadata (Encoding.UTF8)); Private Static voidSetem (DependencyObject obj,encode e) {Switch(e) { CaseEncode.Default:SetEncodingModel (obj, encoding.default); Break; CaseEncode.ASCII:SetEncodingModel (obj, encoding.ascii); Break; CaseEncode.UTF8:SetEncodingModel (obj, Encoding.UTF8); Break; CaseEncode.UTF32:SetEncodingModel (obj, encoding.utf32); Break; CaseEncode.UTF7:SetEncodingModel (obj, encoding.utf7); Break; CaseEncode.BigEndianUnicode:SetEncodingModel (obj, encoding.bigendianunicode); Break; CaseEncode.Unicode:SetEncodingModel (obj, encoding.unicode); Break; default: Break; } } Private Static BOOLIsoutmaxbytelength (stringtxt, DependencyObject obj) { intTxtlength = Getencodingmodel (obj). GetBytes (TXT). Length;//Text Length if(Getmaxbytelength (obj) >=txtlength) {Pretext=txt; return false; } return true; } }
Here's how to use it:
Maxbytelength is a must set not to default, Encodemodel can not be set but because it is our own use, so the default is UTF8, you can modify the code, in accordance with your company's coding format, so you do not have to assign value.
WPF textbox limits input by byte length