Saturday, March 1, 2014

WPF - Ellipsis for Overflow Text in TextBlock

This is a very short post discussing about how to accommodate the overflown text in a text block when we don't want to wrap. Let's see what I am talking about. Here we have a Text Block showing the first name of a user. Since names can be longer than whatever we plan for in this view. We don't want to wrap them into a new line. We also want to show ellipsis at the end to show the presence of hidden text part.



In order to do that, WPF has provided TextTrimming property in TextBlock. In order to get the above result, we can just provide the TextBlock definition as follows:


Here we are using the ellipsis on word boundaries. We can also use it on character boundaries. We just need to set it like this:


And it should show up like this:

2 comments:

Unknown said...

But what if the text has newline.
For your example lets consider the input test as below
"This is
sample text
with enter
button press"

can we make it show like:

This is...

Muhammad Shujaat Siddiqi said...

Hi Shyam, It should show ellipsis for whatever text it is not able to accommodate in the size of the TextBlock. What behaviors do you see for your text?