强制 TextBlock 在 WPF ListBox 中换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/397257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force TextBlock to wrap in WPF ListBox
提问by Eric Haskins
I have a WPF listbox which displays messages. It contains an avatar on the left side and the username and message stacked vertically to the right of the avatar. The layout is fine until the message text should word wrap, but instead I get a horizontal scroll bar on the listbox.
我有一个显示消息的 WPF 列表框。它包含左侧的头像和垂直堆叠在头像右侧的用户名和消息。布局很好,直到消息文本应该自动换行,但我在列表框中得到了一个水平滚动条。
I've Googled and found solutions to similar issues, but none of them worked.
我已经谷歌搜索并找到了类似问题的解决方案,但没有一个奏效。
<ListBox HorizontalContentAlignment="Stretch" ItemsSource="{Binding Path=FriendsTimeline}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border BorderBrush="DarkBlue" BorderThickness="3" CornerRadius="2" Margin="3" >
<Image Height="32" Width="32" Source="{Binding Path=User.ProfileImageUrl}"/>
</Border>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=User.UserName}"/>
<TextBlock Text="{Binding Path=Text}" TextWrapping="WrapWithOverflow"/> <!-- This is the textblock I'm having issues with. -->
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
回答by Nash
Contents of the TextBlock
can be wrapped using property TextWrapping
.
Instead of StackPanel
, use DockPanel
/Grid
.
One more thing - set ScrollViewer.HorizontalScrollBarVisibility
property to Disabled
value for the ListBox
.
的内容TextBlock
可以使用 property 包装TextWrapping
。而不是StackPanel
,使用DockPanel
/ Grid
。另一件事 - 将ScrollViewer.HorizontalScrollBarVisibility
属性设置Disabled
为ListBox
.
Updated Hidden
to Disabled
based on comment from Matt. Thanks Matt.
更新Hidden
到Disabled
基于马特评论。谢谢马特。
回答by Martin Moser
The problem might not be located in the ListBox. The TextBlock won't wrap, if one of the parent controls provides enough space, so that it hasn't the need to wrap. This might be caused by a ScrollViewer control.
问题可能不在 ListBox 中。如果父控件之一提供了足够的空间,则 TextBlock 将不会换行,因此它不需要换行。这可能是由 ScrollViewer 控件引起的。
回答by eldor
If you want to prevent TextBlock to grow, and you want it to just fit in the size of the listbox, you should set the width of it explicitly.
如果你想阻止 TextBlock 增长,并且你想让它刚好适合列表框的大小,你应该明确设置它的宽度。
In order to change it dynamically, it means not a fix value, but you need to bind it to its proper parent element in the visual tree. You can have something like this:
为了动态改变它,它意味着不是一个固定值,但你需要将它绑定到可视化树中的正确父元素。你可以有这样的事情:
<ListBox ItemsSource="{Binding MyItems}" Name="MyListBox">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Width"
Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ScrollContentPresenter}, Path=ActualWidth}" />
</Style>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
If it does not work, try to find the proper elements (which has to be binded to what) with the Live Visual Treein Visual Studio.
如果它不起作用,请尝试使用Visual Studio 中的Live Visual Tree找到正确的元素(必须绑定到什么)。