Rajib_119
Rajib_119

Reputation: 81

Content overflow in Textblock

I am working in a UWP app. For some reason I have to show text within a Textblock & need to know if the content(text) overflows the content areas or not. Is there any event or property of textblock to know this?

<TextBlock 
    Margin="{StaticResource SmallTopMargin}"
    Style="{StaticResource SubtitleTextStyle}"
    Text="{x:Static properties:Resources.SettingsPageAboutTitle}" />

Upvotes: 0

Views: 102

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32785

For some reason I have to show text within a Textblock & need to know if the content(text) overflows the content areas or not.

Sure, you could get the end character rect with GetCharacterRect method, if it's right value more then TextBlock's width property or more than parent container's ActualWidthProperty, it means the text is overflow.

 var endRect = MyTextBlock.ContentEnd.GetCharacterRect(Windows.UI.Xaml.Documents.LogicalDirection.Backward);
 var parentWidth = MyTextBlock.Parent.GetValue(ActualWidthProperty);
 if (MyTextBlock.Width is double.NaN)
 {
     if (endRect.Right > (double)parentWidth)
     {

         // overflow
     }

 }
 else
 {
     if (endRect.Right > MyTextBlock.Width)
     {
         // overflow
     }
 }

Upvotes: 1

Related Questions