53

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

Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
Ramesh Soni
  • 15,867
  • 28
  • 93
  • 113

2 Answers2

114

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

<Label>
     <TextBlock TextTrimming="CharacterEllipsis">Hello World</TextBlock>
</Label>
John Cummings
  • 1,949
  • 3
  • 22
  • 38
Ray
  • 45,695
  • 27
  • 126
  • 169
  • 1
    Hell no! This is what you hope for, but you know they couldn't have made it that easy! Really cool feature! – bas Sep 11 '15 at 21:47
  • For some vague reason (is anything not vague in wpf?) this is working in my designer, but not at runtime – Edwin Mar 04 '17 at 22:17
  • Nevermind, I needed to assign text to the textblock and not use label.Content anymore in my code. – Edwin Mar 04 '17 at 22:34
1

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.

Steven G.
  • 31
  • 2