Auto clip and append dots in WPF label

WpfLabelClipping

Wpf Problem Overview


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

Wpf Solutions


Solution 1 - Wpf

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

<Label>
     <TextBlock TextTrimming="CharacterEllipsis">Hello World</TextBlock>
</Label>

Solution 2 - Wpf

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRamesh SoniView Question on Stackoverflow
Solution 1 - WpfRayView Answer on Stackoverflow
Solution 2 - WpfStefan GintherView Answer on Stackoverflow