How to place an ellipsis in the center of the content of a label or a text of a textbloxk

时间:2015-06-30 13:35:30

标签: c# .net wpf wpf-controls .net-4.5

if i'm using following code

<Stackpanel> <TextBlock TextTrimming="CharacterEllipsis">abcdefghijklmn1234567890</TextBlock> </Stackpanel>

i get following result:

abcdefghijklmn1234...

My question is if it is possible to archive following behaviour:

abcdefghij...234567890

Has anyone an idea how this can work?

2 个答案:

答案 0 :(得分:1)

It will be ugly, but you could find the length of the textblock text then divide this by 2 to find out where the middle of the string is. Then you can take the value of that substring and do a replace of itself with the ellipses added. You'll have to decide how to deal with content that does not have an even number of characters.

答案 1 :(得分:0)

仅使用XAML

<Grid ToolTip="My tooltip">
    <TextBlock Margin="0,0,60,0" TextTrimming="CharacterEllipsis" Text="This is very long line that ended with 123">
    </TextBlock>
    <Grid Width="60" HorizontalAlignment="Right">
        <TextBlock HorizontalAlignment="Right" FlowDirection="RightToLeft" TextAlignment="Left" Text="This is very long line that ended with 123"></TextBlock>
    </Grid>
</Grid>