【问题标题】:Show Ellipsis(...) Button When Text Exceeds Range WPF当文本超出范围 WPF 时显示省略号(...)按钮
【发布时间】:2018-04-30 22:12:53
【问题描述】:

我有一个宽度为 100 的 TextBlock。当文本长度很大时,我想显示该文本块中容纳的字符,并在文本之外显示一个 (...) 按钮以指定用户更多文本是也在那里。单击该 (...) 按钮后,全文将显示在单独的弹出窗口中。

所以我想要在文本长度超过文本块大小时如何显示动态 (...) 按钮。请回答

【问题讨论】:

    标签: c# .net wpf


    【解决方案1】:

    这不是你想要的,但它是一个类似的想法,只是使用了烘焙的东西:

    <TextBlock MaxWidth="200"
               Text="{Binding YourLongText}"
               TextTrimming="WordEllipsis"
               ToolTip="{Binding YourLongText}" />
    

    因此,您有一个具有最大宽度的 TextBlock,当文本无法容纳时,它会显示一个省略号(“...”)。将鼠标悬停在 TextBlock 上会在 ToolTip 中显示全文。

    【讨论】:

    • 感谢马特的回复。但是我想在文本长度超过时自动显示一个按钮。在 TextTrimming ... 出现,但我想要一个具有内容的按钮 ... 应该出现。
    【解决方案2】:

    在按钮上添加省略号的要求相同,因此在此处添加解决方案

    <Style x:Key="editButton" TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Transparent" />                          
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center" >
                                    <ContentPresenter.Resources>
                                        <Style TargetType="TextBlock">
                                            <Setter Property="TextTrimming" Value="CharacterEllipsis"></Setter>
                                        </Style>
                                    </ContentPresenter.Resources>
                                </ContentPresenter>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Transparent"/>                      
                    </Trigger>
                </Style.Triggers>
            </Style>
    

    注意内容展示器中的资源。

    【讨论】:

    • 对于像我这样的其他人:使用 ContentPresenter 而不是 ContentControl。
    【解决方案3】:

    我相信您想要的是将TextTrimming property. Settng 设置为 WordElilipsis 或 CharacterEllipsis 应该提供您需要的内容。

    【讨论】:

      【解决方案4】:

      我对这个问题的解决方案可能有点矫枉过正,但允许进行一些配置和控制。 我创建了一个允许我为每个绑定设置字符限制的行为。

       internal class EllipsisStringBehavior
      {
          public static readonly DependencyProperty CharacterLimitDependencyProperty = DependencyProperty.RegisterAttached("CharacterLimit", typeof(int), typeof(EllipsisStringBehavior), new PropertyMetadata(255, null, OnCoerceCharacterLimit));
          public static readonly DependencyProperty InputTextDependencyProperty = DependencyProperty.RegisterAttached("InputText", typeof(string), typeof(EllipsisStringBehavior), new PropertyMetadata(string.Empty, OnInputTextChanged));
      
          // Input Text
          public static string GetInputText(DependencyObject dependencyObject)
          {
              return Convert.ToString(dependencyObject.GetValue(InputTextDependencyProperty));
          }
          public static void SetInputText(DependencyObject dependencyObject, string inputText)
          {
              dependencyObject.SetValue(InputTextDependencyProperty, inputText);
          }
      
          // Character Limit
          public static int GetCharacterLimit(DependencyObject dependencyObject)
          {
              return Convert.ToInt32(dependencyObject.GetValue(CharacterLimitDependencyProperty));
          }
          public static void SetCharacterLimit(DependencyObject dependencyObject, object characterLimit)
          {
              dependencyObject.SetValue(CharacterLimitDependencyProperty, characterLimit);
          }
      
          private static void OnInputTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              TextBlock textblock = (TextBlock)d;
      
              string input = e.NewValue == null ? string.Empty : e.NewValue.ToString();
              int limit = GetCharacterLimit(d);
      
              string result = input;
      
              if (input.Length > limit && input.Length != 0)
              {
                  result = $"{input.Substring(0, limit)}...";
              }
      
              textblock.Text = result;
          }
      
          private static object OnCoerceCharacterLimit(DependencyObject d, object baseValue)
          {
              return baseValue;
          }
      }
      

      然后我只需将 using 添加到我的用户控件中...

      <UserControl 
       xmlns:behavior="clr-namespace:My_APP.Helper.Behavior"
       d:DesignHeight="300" d:DesignWidth="300">
      

      ...并将行为应用到我希望在其上使用的 TextBlock 控件。

      <TextBlock Margin="0,8,0,8"
       behavior:EllipsisStringBehavior.CharacterLimit="10" 
       behavior:EllipsisStringBehavior.InputText="{Binding Path=DataContext.FeedItemTwo.Body, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
       Style="{StaticResource MaterialDesignSubheadingTextBlock}"  
       FontSize="14"/>
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2018-05-03
        • 1970-01-01
        • 2012-04-13
        • 2016-09-24
        • 2015-09-06
        • 1970-01-01
        • 2018-07-22
        • 1970-01-01
        • 2010-11-27
        相关资源
        最近更新 更多