2016-08-02 30 views
0

我有一个UWP应用程序,我想修剪TextBlock的文本,如果它超出第三行并在第三行显示“show more”链接(可点击)第三行结束。UWP:根据行数修剪TextBlock的文本

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

我该如何实现它?

回答

2

阅读其描述所有步骤的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); 
     } 
    } 
} 
+0

我会试试看。谢谢。 – tavier

+0

如果链接被破坏,请在这里发布必要的部分。您可以保留链接作为参考当然。 – Bart

+0

@Bart更新了帖子 –