Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文本超出WPF范围时显示省略号(…)按钮_C#_.net_Wpf - Fatal编程技术网

C# 文本超出WPF范围时显示省略号(…)按钮

C# 文本超出WPF范围时显示省略号(…)按钮,c#,.net,wpf,C#,.net,Wpf,我有一个宽度为100的文本块。当文本长度较大时,我希望显示文本块中容纳的字符,并在文本之外显示一个(…)按钮,以指定用户还有更多文本。单击该(…)按钮后,全文将显示在单独的弹出窗口中 所以我想知道当文本长度超过textblock的大小时,动态(…)按钮将如何显示。请回答我相信您想要的是将其设置为WordEllipsis或CharacterEllipsis应该提供您需要的内容。这并不完全是您想要的,但这是一个类似的想法,只是使用了烘焙的东西: <TextBlock MaxWidth="200

我有一个宽度为100的文本块。当文本长度较大时,我希望显示文本块中容纳的字符,并在文本之外显示一个(…)按钮,以指定用户还有更多文本。单击该(…)按钮后,全文将显示在单独的弹出窗口中


所以我想知道当文本长度超过textblock的大小时,动态(…)按钮将如何显示。请回答

我相信您想要的是将其设置为WordEllipsis或CharacterEllipsis应该提供您需要的内容。

这并不完全是您想要的,但这是一个类似的想法,只是使用了烘焙的东西:

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


因此,您有一个最大宽度的文本块,当文本不适合时,它会显示一个省略号(“…”)。用鼠标悬停在文本块上,将在工具提示中显示全文。

只需在按钮上添加省略号,就可以在此处添加解决方案

<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>


请注意content presenter中的参考资料。

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

 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"/>


希望这能有所帮助。

感谢马特的回复。但是我想在文本长度超过时自动显示一个按钮。在文本修剪中。。。出现,但我想要一个包含内容的按钮。。。应该显示。对于像我这样的其他人:使用ContentPresenter而不是ContentControl。