Can you limit the length of Text visible in a WPF TextBlock?
Asked Answered
N

2

4

I have a listbox that is bound to a database query result. I'm using an item template that shows the subject on one line and I want it to show preview of the body on another line. What I'm wondering is that-- obviously the body is going to be way too long to fit in there, can I somehow set it to only display the first so many characters and append an ellipses after that, like a preview? Or even something close would be fine. So for example:

Instead of displaying:

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.

It would display something like

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

any ideas?

Notify answered 24/8, 2011 at 5:25 Comment(0)
H
12

You can use 'TextTrimming' Property of a Textblock. Set TextTrimming = "CharacterEllipsis". You might need to play with Width to manage how many characters you really want to display.

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

Add this textblock in your item template

Hathaway answered 24/8, 2011 at 5:34 Comment(1)
i found that almost immediately after posting the question. however Im running into the problem that if there are carriage returns, it will trim each line, but still display each line. in these cases, i only want to show the first line. is that possible?Notify
B
6

Of course!

<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();
    }
}

Looks like:

Shot

Best of luck!

Boarish answered 24/8, 2011 at 5:39 Comment(2)
The problem with using TextTrimming, by the way, is it is based on the size of the TextBlock and not the length of the text. Sometimes this is exactly what you want, I realize. But in the case of your question, I believe you were wanting to base it on the length of the text.Boarish
i see where you went with that and i know thats what im going to end up wanting in the future, for sure. But for the moment, i'd like it to be based on the text block size. that way if i want to preview just a little more, i can drag it out. but im going to dig into your example definitely. thanks!Notify

© 2022 - 2024 — McMap. All rights reserved.