Search code examples
wpfrichtextboxellipsis

How to put ellipsis (...) in RichTextBox


I want to put an ellipsis (..) in the rich text box.

In normal TextBlock, TextTrimming="WordEllipsis" has a property that limits the length to allow ellipsis representation, but rich text boxes do not. And it should only be implemented as a rich textbox now. Text blocks are not allowed.

I'd like to trim by two or three lines and add an ellipsis (..) option. Is there any good way?

I want to show you how I'm using RichTextBox, but the reputation is low.


Solution

  • You don't

    The ellipsis concept is, as stated by grek40, something that only works when displaying text. As an example, say the ellipsis is displayed, and the user tries to partially select some text in your RichTextBox, including the ellipsis, what would the selected result be? You can't tell.

    Maybe

    Since an ellipsis is usually a replacement for a Scrollbar, hiding text instead of allowing you to access it by scrolling, you might be able to fake it by using a WPF Style. Create a Style that displays an icon/picture of an ellipsis (placement is up to you) whenever the Scrollbar visibility trigger is triggered. You would need to disable the Scrollbar once the ellipsis is visible. This obviously requires more effort than simply setting a property, and it can easily become a User Experience nightmare if not carefully implemented, so be warned.

    Note: Another comment (by Walt Ritscher) linked to a similar question, the solution there is similar to this one.

    Alternative Maybe

    Another faked ellipsis could be achieved by using two different RichTextBox controls. The first RichTextBox would have set ReadOnly to true. Trim your text to a maximum allowed length, and append an ellipsis and display it inside this first RichTextBox. When the user need to edit the text hide the first RichTextBox and display a second RichTextBox that contains your entire text.