Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the maximum chr length of a TextBlock in XAML

Tags:

xaml

How would I set the number of characters a user is allowed to input in a TextBlock in xaml?

Would I do it on the Model or create some sort of custom attribute to handle it?

like image 517
griegs Avatar asked Mar 20 '12 00:03

griegs


People also ask

How to limit TextBox length in WPF?

The IsReadOnly property of the TextBox sets the text box read only. By default, it is false. The MaxLength property of the TextBox sets the number of characters allowed to input in a text box.

Is TextBlock editable?

TextBlock is not editable.

What is WPF TextBlock?

The WPF TextBlock control is a lightweight text editor control for displaying and formattting small amount of text flow content. The code examples in this tutorial demonstrates how to use a TextBlock control in WPF using XAML and C#. Creating a TextBlock. The TextBlock element represents a WPF TextBlock control in XAML ...


1 Answers

TextBlock doesn't have a MaxLength, neither does Label. TextBox does. Users cannot input into a TextBlock unless you have modified it.

Is it really a TextBlock you want to limit or did you mean a TextBox? If it is a TextBox you can simply use the MaxLength property.

<TextBox Name="textBox1" MaxLength="5" />

If it really is a TextBlock you are using and somehow allowing a user to input data into it, then switch to use a TextBox. If it is the TextBlock style you are after, you can style the TextBox to look like a TextBlock.

like image 125
Rhyous Avatar answered Oct 27 '22 13:10

Rhyous