【问题标题】:Display only two lines for a long text in a column一列中的长文本仅显示两行
【发布时间】:2018-07-28 05:49:05
【问题描述】:

我在DataGrid 中有一个专栏来显示一条短信。不幸的是,它太长了。所以我在textblock使用TextWrapping = "Wrap"自定义单元格列模板

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

有没有办法做到这一点?

【问题讨论】:

  • 你试过使用 MaxHeight 和 TextTrimming 吗?
  • 难的是如果我改变字体大小,我不知道MaxHeight是否有效?
  • 已编辑,实际上它在 DataGrid 的列中。

标签: wpf xaml textblock


【解决方案1】:

要实现这一点,您需要定义一个自定义 Behavior,首先确保添加 System.Windows.Interactivity 命名空间(它是 Expression.Blend.Sdk 的一部分 em>,使用 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 绑定到具有“Name”属性的TestClassObservableCollectionNumLinesBehaviour 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”。

更新

输出看起来像这样:

【讨论】:

  • 我使用了 g t 的解决方案。但我有一个例外。 NaN is not a valid value for property 'MaxHeight'.
  • 我不确定您指的是什么解决方案,上述行为应该可以完美运行,您是否在实现中进行了任何更改?
猜你喜欢
  • 2021-05-28
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 2018-01-10
  • 2017-12-07
  • 1970-01-01
  • 2021-12-17
相关资源
最近更新 更多