Can you limit the length of text visible in a WPF text block?

I have a list attached to the result of a database query. I am using an element template that shows an object on one line, and I want it to show a preview of the body on another line. I’m interested in the fact that ... it’s obvious that the body will be too long to fit into it, can I somehow adjust it to display only the first so many characters and add ellipses after this, like the preview? Or even something close would be good. For example:

Instead of displaying:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse vitae eros nibh. Resident of Pellentesque morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec augue metus, iaculis id porta non, pellentesque quis turpis. Donec rutrum diam eget tortor bibendum vel blandit odio iaculis. Curabitur pretium adipiscing orci, ut pulvinar justo automobileula non. Maurice Taco. Sed et auctor nibh. Proin ac ultricies tellus.

It will display something like

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ...

any ideas?

+4
source share
2 answers

You can use the TextTrimming property for a text block. Set TextTrimming = "CharacterEllipsis". You may need to play with Width to control the number of characters you really want to display.

<TextBlock TextTrimming="CharacterEllipsis" Text="This is a sample long text. This will get Trimmed."/> 

Add this text block to your element template.

+11
source

Sure!

 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <sys:String x:Key="MyData">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse vitae eros nibh. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec augue metus, iaculis id porta non, pellentesque quis turpis. Donec rutrum diam eget tortor bibendum vel blandit odio iaculis. Curabitur pretium adipiscing orci, ut pulvinar justo vehicula non. Mauris nec ipsum velit. Sed et auctor nibh. Proin ac ultricies tellus.</sys:String> <local:MyTruncateConverter x:Key="MyConverter" /> </Window.Resources> <TextBlock Text="{Binding Source={StaticResource MyData}, Converter={StaticResource MyConverter}, ConverterParameter=50}" TextWrapping="Wrap" /> </Window> 

Then use this converter:

 public class MyTruncateConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return string.Empty; if (parameter == null) return value; int _MaxLength; if (!int.TryParse(parameter.ToString(), out _MaxLength)) return value; var _String = value.ToString(); if (_String.Length > _MaxLength) _String = _String.Substring(0, _MaxLength) + "..."; return _String; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } 

It looks like:

Shot

Good luck

+6
source

All Articles