尝试将MaxLines功能添加到TextBlock中是否更好,还是将TextTrimming添加到TextBox中? 我看到了一些与我需要做的事情非常接近的帖子。 这里有一个在编辑时将TextTrimming添加到TextBox中,但不清楚MaxLines是否仍然有效: TextBox TextTrimming。 我没有找到任何关于将MaxLines属性添加到TextBlock中的内容。 请注意,Windows手机的TextBlock似乎具有此功能。 也许我不是唯一需要这个的人 :)
我有点惊讶这不是“开箱即用”的。 这似乎是一个常见的问题。 顺便说一下,对于TextBox,TextBlock甚至标签或其他东西都没有偏好。 这些只是ListBox项目,不可编辑。
非常感谢任何想法或指针。
- Dave *如果你在想,“他可能会接下来问如何在用户悬停在缩写的笔记上时显示弹出窗口”,那么你是正确的!
以下是一种处理该问题的方法,基于此stackoverflow帖子:Scrollable TextBlock Sized EXACTLY 2 Lines High,请参见Joel B Fant的答案。 关键思想是另一个不可见的TextBlock(下面称为“限制器”)具有2行(或您想要的任何内容)。 然后,将文本块的高度属性绑定到虚拟“限制器”文本块的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>
PropertyMetaData
,为UserControl.FontProperty DependencyProperty
添加一个PropertyChangedCallback
处理程序,每次更改时都可以测量文本高度取决于新的FontFamily
值。 - Sheridan