Alex Rivera | Logout

Get Displayed Text from TextBlock

Asked 2010-11-30T21:47:54.737
25

I have a simple TextBlock defined like this

<StackPanel>
    <Border Width="106"
            Height="25"
            Margin="6"
            BorderBrush="Black"
            BorderThickness="1"
            HorizontalAlignment="Left">
        <TextBlock Name="myTextBlock"
                   TextTrimming="CharacterEllipsis"
                   Text="TextBlock: Displayed text"/>
    </Border>
</StackPanel>

Which outputs like this

alt text

This will get me "TextBlock: Displayed text"

string text = myTextBlock.Text;

But is there a way to get the text that's actually displayed on the screen?
Meaning "TextBlock: Display..."

Thanks

Edit
Report

1 Answer

10

After rooting around I Reflector for a while, I found the following:

System.Windows.Media.TextFormatting.TextCollapsedRange 

which has a Length property that contains the number of characters that are NOT displayed (are in the collapsed/hidden portion of the text line). Knowing that value, it's just a matter of subtraction to get the characters that ARE displayed.

This property is not directly accessible from the TextBlock object. It looks like it is part of the code that is used by WPF to actually paint the text on the screen.

It could end up being quite a lot of fooling around to actually get the value of this property for the text line in your TextBlock.

answered 2010-12-05T11:24:36.083

Your Answer