列中的长文本仅显示两行

Display only two lines for a long text in a column

我在 DataGrid 中有一个列用于显示文本消息。不幸的是它太长了。所以我在 textblock 中自定义单元格列模板使用 TextWrapping = "Wrap"

显示多行。我不想要它。我只想显示前两行,最后添加一个省略号(...)

有办法吗?

为此,您需要定义自定义 Behavior,首先确保添加 System.Windows.Interactivity 命名空间(它是 Expression.Blend.Sdk,使用 NuGet 安装它:Install-Package Expression.Blend.Sdk),这里是一个基本的实现(信用转至 @Itzalive):

public class NumLinesBehaviour : Behavior<TextBlock>
    {
        public static readonly DependencyProperty MaxLinesProperty =
            DependencyProperty.RegisterAttached(
                "MaxLines",
                typeof(int),
                typeof(NumLinesBehaviour),
                new PropertyMetadata(default(int), OnMaxLinesPropertyChangedCallback));

        public static void SetMaxLines(DependencyObject element, int value)
        {
            element.SetValue(MaxLinesProperty, value);
        }

        public static int GetMaxLines(DependencyObject element)
        {
            return (int)element.GetValue(MaxLinesProperty);
        }

        private static void OnMaxLinesPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is TextBlock element) element.MaxHeight = GetLineHeight(element) * GetMaxLines(element);
        }

        public static readonly DependencyProperty MinLinesProperty =
            DependencyProperty.RegisterAttached(
                "MinLines",
                typeof(int),
                typeof(NumLinesBehaviour),
                new PropertyMetadata(default(int), OnMinLinesPropertyChangedCallback));

        public static void SetMinLines(DependencyObject element, int value)
        {
            element.SetValue(MinLinesProperty, value);
        }

        public static int GetMinLines(DependencyObject element)
        {
            return (int)element.GetValue(MinLinesProperty);
        }

        private static void OnMinLinesPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is TextBlock element) element.MinHeight = GetLineHeight(element) * GetMinLines(element);
        }

        private static double GetLineHeight(TextBlock textBlock)
        {
            double lineHeight = textBlock.LineHeight;
            if (double.IsNaN(lineHeight))
                lineHeight = Math.Ceiling(textBlock.FontSize * textBlock.FontFamily.LineSpacing);
            return lineHeight;
        }
    }

现在假设你有一个 DataGrid 绑定到 TestClassObservableCollection 与“Name” 属性,基本用法NumLinesBehaviour Behavior 如下:

<Window ...   
    xmlns:local="clr-namespace:YourNameSpace"       
    Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <DataTemplate x:Key="CellTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock 
                Width="200"
                TextWrapping="Wrap" 
                local:NumLinesBehaviour.MaxLines="2"
                TextTrimming="WordEllipsis" 
                LineStackingStrategy="BlockLineHeight"
                Text="{Binding Name}"/>

        </StackPanel>
    </DataTemplate>
</Window.Resources>

<Grid>
    <DataGrid ItemsSource="{Binding DgCollection}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Name" CellTemplate="{StaticResource CellTemplate}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

确保将 TextBlockTextTrimming 设置为“WordEllipsis”。

更新

输出看起来像这样: