Remaining Characters in TextBox in Silverlight 5

In this article we would see, how we can limit the TextBox with remaining characters available to type in. This is a beginner’s article, where you might find some tricks for Binding, using Converters.

We would startup by creating a sample project.

As a beginning we would create a TextBox and a TextBlock to display the remaining characters.

As you see above the MaxLenght of the TextBox is 150. That is 150 characters.

Now we need to have a converter which would give us the remaining characters.

As you see in above converter, we are accessing a Constant Value from the App. You could define your own at specific helper classes.

Also we are returning a string.

Now we would do the Binding with the TextBlock, to display number of characters remaining.

As you see above, we have done Element-To-Element Binding, and we used a converter; which is discussed above.

Now let’s see how it is working.

Although we have achieved the goal of displaying remaining characters. Here are some tips/tricks for binding.

Let’s say you need to display multiple texts in the TextBlock, with Binding Text as well.

As you see above, we have a static Text of “Remaining Characters: ” and we have bound the next Text. As we have a static text, we might need to change the converter to return the integer value of remaining characters only. As follows:

Here is another trick: Binding multiple items in a TextBlock.

As you see above, we have multiple bindings for a single TextBlock. Which would reduce number of TextBlocks in your XAML?

The final output might look like below.

Hope this article helps, thanks for reading.

Source Code: Click Here

Article as PDF: Click Here

Tryout my Codeplex Projects:

  1. Visual Studio Cleaner
  2. Auto Power Switch
  3. Auto Wall
  4. Application Scheduler
  5. Windows 8 Storage Helper