wpf - 需要带有 TextTrimming 和 MaxLines 的 WPF TextBlock 或 TextBox

标签 wpf textbox listbox styles textblock

我需要一个具有 TextTrimming 和 MaxLines 的 TextBlock 或 TextBox。 TextBlock 具有 TextTrimming 属性,TextBox 具有 MaxLines 属性,但两者都不支持。这是一个更大的控件的一部分,允许用户输入注释并将它们显示在 ListBox 中(如果有兴趣,请参阅:Grief with WPF UserControl with ListBox and TextBox Can't get textwrapping to work)。基本上,对于已经输入的注释,我想将注释限制为 2 行。如果它长于 2 行,我会在它的末尾显示一个椭圆 (...) 以表明该注释还有更多内容。如果用户将鼠标悬停在缩写注释上,他/她会将整个注释视为弹出窗口。* 控件将放置在主网格上,并且可以具有不同的大小(或调整大小),因此 2 行中的文本量可以改变. IE。在某些情况下可以完全显示的注释可能最终在其他情况下被缩写。

尝试将 MaxLines 功能添加到 TextBlock 或将 TextTrimming 添加到 TextBox 会更好吗?我看过一些似乎接近我需要做的帖子。这是一个将 TextTrimming 添加到 TextBox 但仅在编辑时且 MaxLines 是否仍然有效的方法:TextBox TextTrimming
我没有遇到任何关于将 MaxLines 属性添加到 TextBlock 的内容。请注意,Windows 手机的 TextBlock 似乎有它。也许我不是唯一需要这个的人:)

我有点惊讶这不是“开箱即用”的。这似乎是一个普遍的问题。顺便说一句,对于 TextBox、TextBlock,甚至是 Label 或其他东西,都没有偏好。这些只是 ListBox 项目,不可编辑。

任何想法或指针都非常感谢。

-戴夫
*如果你在想,“他可能接下来会问如何在用户将鼠标悬停在缩写的注释上时显示一个弹出窗口”,你是完全正确的!

这是基于此stackoverflow帖子处理问题的方法:
Scrollable TextBlock Sized EXACTLY 2 Lines High
请参阅 Joel B Fant 的回答。关键思想是另一个不可见的 TextBlock(下面称为“限制器”),它有 2 行(或任何你想要的)。然后,将文本 block 的 Height 属性绑定(bind)到虚拟“limier”文本 block 的 ActualHeight。这是我的 XAML 的样子,它似乎可以工作:

<UserControl x:Class="MyControlsLibrary.Views.NotesControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

<UserControl.Resources>
    <DataTemplate x:Key="DefaultTemplate">
        <Grid x:Name="GridItem" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition   />   

            </Grid.ColumnDefinitions>
            <ScrollViewer Margin="0,5,5,0" MaxHeight="{Binding ElementName=limiter,Path=ActualHeight}" HorizontalAlignment="Stretch" VerticalAlignment="Top" VerticalScrollBarVisibility="Hidden">
                <TextBlock   x:Name="NoteText" Grid.Column="0"  Height="{Binding ElementName=limiter,Path=ActualHeight}"  Text="{Binding Path=NoteText}"   TextTrimming="WordEllipsis" TextWrapping="Wrap">
                    <TextBlock.ToolTip>
                        <TextBlock Text="{Binding Path=NoteText}" TextWrapping="Wrap"></TextBlock>
                    </TextBlock.ToolTip>
            </TextBlock>
            </ScrollViewer>
            <TextBlock x:Name="limiter" Grid.Column="0"  Margin="0,5,5,0" Visibility="Hidden" HorizontalAlignment="Left" Width="10"  VerticalAlignment="Top">
            a <LineBreak/> b
            </TextBlock>

        </Grid>
    </DataTemplate>
</UserControl.Resources>
<Grid Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBox TextWrapping="Wrap" Grid.Row="0"  Text="{Binding Path=NewNoteText, UpdateSourceTrigger=PropertyChanged}"   LostFocus="TextBox_LostFocus" AcceptsReturn="True">
        <TextBox.InputBindings>
            <KeyBinding Command="{Binding Path=AddNote}" Key="Enter"/>
        </TextBox.InputBindings>
    </TextBox>

    <ListBox     
        ScrollViewer.HorizontalScrollBarVisibility="Disabled"  Grid.Row="1"  ItemsSource="{Binding Path=Notes}"  Margin="5" ItemTemplate="{DynamicResource DefaultTemplate}"   SelectionChanged="NotesControl_SelectionChanged">
        </ListBox>


</Grid>

最佳答案

您可以从默认的 TextBlock 中获得该功能。 .而不是使用 MaxLines属性,您可以使用普通的旧 Height属性(property)。这是一个非常简单的例子:

<TextBlock Text="This is some very important note which just happens to be quite long" 
    Height="38" Width="150" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" />

enter image description here

请记住,我只设置了 Width这里向您展示带有省略号的两行文本。在您的项目中,您应该让 Grid设置为 Width .

当然你不能用它来输入文本,所以如果你有兴趣,可以下载一个TextBox带有WPF TextBox With Ellipsis 中的省略号代码项目页面。

关于wpf - 需要带有 TextTrimming 和 MaxLines 的 WPF TextBlock 或 TextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21892728/

相关文章:

C# WPF - 动态 TabControl 中的滚动条

forms - 根据表单中的多个列表框过滤查询

asp.net-mvc-3 - MVC3 Razor - 列表框预选不起作用

wpf - 使用模板绑定(bind)列表框项目

c# - 画线和时的奇怪行为。矩形

c# - 向 WPF 窗口添加控件而不阻塞 GUI 线程

wpf - 使用 anchor 计算比例

c# - 在 TextBox 控件中创建超链接

c# - 在富文本框中显示行号c#

asp.net - 如何设置多行文本框的最大长度?