0

I have some TextBlock, that can display very long string.

However, when TextBlock content width become greater than TextBlock own width, i want to cut some part of content and place "..." at the end instead.

I will display full content as TooTtip.

<TextBlock Text="{Binding Path=MyValue}" TextAlignment="Right" FontWeight="Bold" ToolTip="{Binding MyValue}" /> 

As far as i know TextBlock have no events, that can fire after content changed. Is there any good approach to listen something like contentChanged event on TextBlock, calculate text width, compare with control width and cut part of content that not fit?

I need to do it in codebehind, because viewmodel cant be changed some reasons that not matter.

2
  • 4
    Why not just set TextTrimming no either CharacterEllipsis or WordEllipsis? Commented Aug 19, 2015 at 9:31
  • You can write a converter which would contain that logic and pass right data to TextBlock. Commented Aug 19, 2015 at 9:31

1 Answer 1

2

You can use TextTrimming:

https://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.texttrimming(v=vs.110).aspx

<TextBlock Text="{Binding Path=MyValue}" TextAlignment="Right" FontWeight="Bold" ToolTip="{Binding MyValue}" TextTrimming="WordEllipsis" /> 

or you can set it in codebehind:

myTextBlock.TextTrimming = TextTrimming.CharacterEllipsis; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.