Reputation: 20384
There is a TextBlock element in my WPF view and it is data-bound to a string property in the view model. Now the text in this property can get a bit longer so it should be trimmed to be displayed in a single line. The layout should not be changed, so using multiple lines must be avoided. Also the data binding source should not be changed and remain a single string. The text to be displayed looks like the following examples:
The simple solution is this:
<TextBlock Text="{Binding PageTitle}" TextTrimming="CharacterEllipsis"/>
This works fine for the first two example texts, but in the last two samples, I would like the closing parenthesis to remain visible. So what I currently get is this:
But what I seek for is this:
Is there a simple way in WPF to do that? Basically, the ellipsis string, which is now always three dots (visually, might also be the Unicode character for that), needs to be variable and include whatever closing punctuation is included in the source string.
I could write a custom control for that, if necessary, but need help on the implementation of the trimming, too.
Upvotes: 1
Views: 3614
Reputation: 1789
Simple approach: use a DockPanel
, put two TextBlock
s in it, the first one displays your string with a ellipsis trimming, and the second one showing the closing parenthesis, docked to the right:
<DockPanel>
<TextBlock Text=")" DockPanel.Dock="Right" />
<TextBlock Text="{Binding PageTitle}" TextTrimming="CharacterEllipsis" />
</DockPanel>
If you want to do the trimming yourself, you can have a look at my answer in this post: Ellipsis at start of string in WPF ListView.
Upvotes: 2
Reputation: 38
I don't have an implementation for you, but since you're using binding, I'd consider using a converter rather than a custom control. You have access to everything you need in the args to the convert method to calculate how much of the string to display and complete control over how it displays without having to modify the data in the view model.
Upvotes: 0