1

I'm using RichTextBlock in Windows Phone 8.1 RT to show some text. To limit the size of the text that can be displayed at a given time, I'm setting the MaxLines property. Whenever the text exceeds this value, it's trimmed.

Now, I have a hyperlink at the bottom of the RichTextBlock that should get visible whenever the text is trimmed. For detecting if the text was trimmed, I'm using the RichTextBlock.HasOverflowContent. If this property is set to true, I set to visibility of the hyperlink to visible so the user can click on it and see the full untrimmed message.

But there is a problem with this solution. Sometimes the text is trimmed, but the property is still false and the hyperlink stays hidden.

I don't really know how to use the above property to detect content trimming. What's the correct way to use this? I'm doing the processing in the Loaded event of the RichTextBlock:

private void RichTextBlock_Loaded(object sender, RoutedEventArgs e)
{
    var richtextblock = sender as RichTextBlock;

    // Check if the content of the RichTextBlock was trimmed.
    if (richtextblock.HasOverflowContent)
    {
        // Prepare hyperlink and set visibility to visible.
    }
}
nimbudew
  • 958
  • 11
  • 28

1 Answers1

2

Instead of checking the value of HasOverflowContent when the RichTextBlock is loaded, why don't you try to bind the visibility property of the hyperlink to the HasOverflowContent property (using a Boolean To Visibility converter of course)?

Jogy
  • 2,465
  • 1
  • 14
  • 9
  • I'm using the `RichTextBlock` in a user control, and cannot implement `INotifyPropertyChanged` to bind the property to UI acc. to this question: http://stackoverflow.com/questions/12466216/inotifypropertychanged-in-usercontrol. – nimbudew Nov 23 '15 at 11:20
  • 1
    You do not need INotifyPropertyChanged. Just give the RichTextBox a name, then bind using ElementName: {Binding ElementName=richTextBlock, Path=HasOverflowContent, Converter=...}" – Jogy Nov 23 '15 at 12:08
  • the visibility part works fine now, thanks to the binding. But I also do some processing in the `if` block (text formatting using `Run` and `Underline` for hyperlink creation). I don't want to do that processing for each textblock, but only for the ones where the text is trimmed. Where should I ideally access `HasOverflowContent`, because the `Loaded` event gives incorrect values sometimes? – nimbudew Nov 23 '15 at 13:16
  • Check for example this link for a class allowing you to subscribe to changes in a DependencyProperty: http://www.thomaslevesque.com/2013/04/21/detecting-dependency-property-changes-in-winrt/ – Jogy Nov 23 '15 at 13:27