Search code examples
xamlwindows-phone-7textblock

Limit number of characters on TextBlock


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


Solution

  • 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;