Auto clip and append dots in WPF label
Asked Answered
P

2

55

How can I auto clip text and append dots on a label if the current text doesn't fits to its width in WPF?

Prentiss answered 8/6, 2010 at 9:36 Comment(0)
D
116

Put a TextBlock inside your label and set TextTrimming to CharacterEllipsis or WordEllipsis

<Label>
     <TextBlock TextTrimming="CharacterEllipsis">Hello World</TextBlock>
</Label>
Downtoearth answered 8/6, 2010 at 9:41 Comment(3)
Hell no! This is what you hope for, but you know they couldn't have made it that easy! Really cool feature!Adroit
For some vague reason (is anything not vague in wpf?) this is working in my designer, but not at runtimePeer
Nevermind, I needed to assign text to the textblock and not use label.Content anymore in my code.Peer
R
2

It's also possible to use AccessText within the Label like this:

<StackPanel Orientation="Horizontal">
<Label VerticalAlignment="Center" Width="50"
        Target="{Binding ElementName=txtName}">
    <AccessText Text="_First Name" TextTrimming="CharacterEllipsis"  />
</Label>
<TextBox Name="txtName" VerticalAlignment="Center" Width="120"/>

With this solution the access key for a control (e.g. Alt+F) still works.

Rum answered 7/3, 2019 at 9:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.