I have limited space to display a title in quotes:
'This is a short title'
so if the title is too long it needs to be truncated and the missing characters indicated by an ellipsis:
'This is too long so is trun...'
I originally had a converter that counted the characters and returned a quoted string but that produces inconsistent results due to the use of proportional fonts. The best I have come up with so far is:
<TextBlock Grid.Column="3"
Margin="5"
Text="{Binding Title, Mode=OneWay, StringFormat='{}\'{0}\''}"
TextTrimming="CharacterEllipsis">
but this just displays:
'This is too long so is trun...
with the closing ' missing. I've tried negative margins and setting the Padding on the TextBlock but all that does is move where the string is truncated and still leaves off the closing '.
The TextBlock is in a Grid that is stretched across the width of the window and there are other UI elements to the right of the title that can't be moved.
I can't use a StackPanel to display the text in three parts (the opening ', the text, the closing ') as that foils the truncation completely as it now thinks it has enough space to display the whole text. I tried using a DockPanel:
<DockPanel Grid.Column="3"
Margin="5">
<TextBlock Text="'"/>
<TextBlock Text="{Binding AlbumName, Mode=OneWay}"
TextTrimming="CharacterEllipsis"/>
<TextBlock Text="'"/>
</DockPanel>
Which worked some of the time. Depending on the title I'd sometimes get the closing ' if the truncated text ended up that little bit shorter. However, I can't use this because if the title is short enough not to need truncating I get:
'This is short '
with a space between the title and the closing '
I've looked into the possibility of passing the column width into the converter but haven't discovered a way of doing that - unless I've been searching using the wrong terms.
Is there any way I can get the effect I desire?