1

How can we limit the number of characters that we want to display on our TextBlock with Windows Phone 7?

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
MarTech
  • 115
  • 3
  • 12
  • Does this answer your question? [Can you limit the length of Text visible in a WPF TextBlock?](https://stackoverflow.com/questions/7170817/can-you-limit-the-length-of-text-visible-in-a-wpf-textblock) – StayOnTarget May 05 '20 at 19:21

3 Answers3

3

You have a couple of options.

  1. You could set the MaxWidth and MaxHeight properties of your TextBlock. Any remaining text would get truncated.
  2. Substring the text before assigning it to the TextBlock. For example:
    var str = "SomeReallyLongString";
    var maxLength = 10;
    yourTextBlock.Text = str.Length > maxLength ? str.Substring(0, maxLength) : str;
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • It works, thanks.But still have a problem; it displays the last characters("LongString") and i want the inverse (SomeReally") ?! – MarTech Dec 29 '11 at 19:29
  • Can you please help me out with my parsing problem? [link](http://stackoverflow.com/questions/8666983/parsing-issues-with-windows-phone7/8667178#8667178) – MarTech Dec 29 '11 at 19:39
1

You could use a read-only textbox instead and set its MaxLength property. Alternatively you could also handle the TextInput event and truncate the text if it's longer than the max length.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
1

Use a converter if you are binding a value to your textblock. You can assign a value (parameter) to the converter to set the max number of characters. This will work beter then maxheight and maxwidth because it wont bother about the font size.

invalidusername
  • 912
  • 9
  • 26