【问题标题】:UWP: Trimming the text of a TextBlock based on number of linesUWP:根据行数修剪 TextBlock 的文本
【发布时间】:2016-08-02 07:48:59
【问题描述】:

我有一个 UWP 应用程序,如果 TextBlock 超出第三行,我想在其中修剪文本,并在第三行末尾显示“显示更多”链接(可点击)。

我知道限制我可以使用 MaxLines 属性的行数,但它只是忽略了其余的行,就好像它们不存在一样。但我想让用户知道还有一些文本,他可以点击显示更多链接以导航到全文。

我怎样才能实现它?

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    阅读good topic,其中描述了创建可扩展文本块的所有步骤

    另外,在github查看源代码

    这是 XAML 代码:

     <Grid x:Name="LayoutRoot" Tapped="LayoutRoot_OnTap">
         <Grid.RowDefinitions>
              <RowDefinition Height = "Auto" />
              <RowDefinition Height="Auto" />
          </Grid.RowDefinitions>
    
          <TextBlock Grid.Row="0" 
                      x:Name= "CommentTextBlock"
                      HorizontalAlignment= "Left"
                      TextWrapping= "Wrap"
                      Height= "Auto"
                      Width= "280" />
    
          < StackPanel Grid.Row= "1"
                      Orientation= "Horizontal"
                      HorizontalAlignment= "Right"
                      x:Name= "ExpandHint"
                      Visibility= "Collapsed"
                      Margin= "0,5,0,0" >
              < TextBlock  Text= "View More" />
               < TextBlock Margin= "10,0,10,0"
            Text= "+" />
          </ StackPanel >
    </ Grid >
    

    这里是 C# 部分

    public sealed partial class ExpandableTextBlock : UserControl
    {
       public ExpandableTextBlock()
        {
            this.InitializeComponent();
        }
    
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            "Text", typeof(string), typeof(ExpandableTextBlock), new PropertyMetadata(default(string), OnTextChanged));
    
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
    
        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctl = (ExpandableTextBlock)d;
            ctl.CommentTextBlock.SetValue(TextBlock.TextProperty, (string)e.NewValue);
            ctl.CommentTextBlock.SetValue(TextBlock.HeightProperty, Double.NaN);
    
            ctl.CommentTextBlock.Measure(new Size(ctl.CommentTextBlock.Width, double.MaxValue));
    
            double desiredheight = ctl.CommentTextBlock.DesiredSize.Height;
            ctl.CommentTextBlock.SetValue(TextBlock.HeightProperty, (double)63);
    
            if (desiredheight > (double)ctl.CommentTextBlock.GetValue(TextBlock.HeightProperty))
            {
                ctl.ExpandHint.SetValue(StackPanel.VisibilityProperty, Visibility.Visible);
                ctl.MaxHeight = desiredheight;
            }
            else
            {
                ctl.ExpandHint.SetValue(StackPanel.VisibilityProperty, Visibility.Collapsed);
            }
    
            //Setting length of comments
            var boundsWidth = Window.Current.Bounds.Width;
            ctl.CommentTextBlock.SetValue(TextBlock.WidthProperty, boundsWidth);
        }
    
        public static readonly DependencyProperty CollapsedHeightProperty = DependencyProperty.Register(
            "CollapsedHeight", typeof(double), typeof(ExpandableTextBlock), new PropertyMetadata(default(double), OnCollapsedHeightChanged));
    
    
        public double CollapsedHeight
        {
            get { return (double)GetValue(CollapsedHeightProperty); }
            set { SetValue(CollapsedHeightProperty, value); }
        }
    
        private static void OnCollapsedHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctl = (ExpandableTextBlock)d;
            ctl.CollapsedHeight = (double)e.NewValue;
        }
    
    
        public static readonly DependencyProperty TextStyleProperty = DependencyProperty.Register(
           "TextStyle", typeof(Style), typeof(ExpandableTextBlock), new PropertyMetadata(default(Style), OnTextStyleChanged));
    
        public Style TextStyle
        {
            get { return (Style)GetValue(TextStyleProperty); }
            set { SetValue(TextStyleProperty, value); }
        }
    
        private static void OnTextStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctl = (ExpandableTextBlock)d;
            ctl.CommentTextBlock.SetValue(StyleProperty, (Style)e.NewValue);
        }
    
        private void LayoutRoot_OnTap(object sender, TappedRoutedEventArgs tappedRoutedEventArgs)
        {
           if ((Visibility)this.ExpandHint.GetValue(StackPanel.VisibilityProperty) == Visibility.Visible)
            {
                //transition
                this.CommentTextBlock.SetValue(TextBlock.HeightProperty, Double.NaN);
    
                this.ExpandHint.SetValue(StackPanel.VisibilityProperty, Visibility.Collapsed);
            }
        }
    }
    

    【讨论】:

    • 我会试一试的。谢谢。
    • 请在此处发布重要部分,以防链接损坏。当然,您可以保留链接作为参考。
    • @AndriiKrupka 我对 CommentTextBlock 的固定宽度有疑问。正如我所说,我在桌面上有一个 UWP 应用程序,如果我调整窗口大小以使其变窄,文本块将切断字符串,因为 CommentTextBlock 的宽度将设置为更宽屏幕的窗口大小。如何固定宽度?
    猜你喜欢
    • 2012-11-18
    • 1970-01-01
    • 2018-02-12
    • 2013-06-30
    • 1970-01-01
    • 2010-11-05
    • 2017-11-07
    • 2019-01-15
    • 1970-01-01
    相关资源
    最近更新 更多