强制将TextBlock包装在WPF ListBox中 [英] Force TextBlock to wrap in WPF ListBox

查看:151
本文介绍了强制将TextBlock包装在WPF ListBox中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示消息的WPF列表框.它在左侧包含一个化身,并在该化身的右侧垂直堆叠了用户名和消息.在消息文本自动换行之前,布局是可以的,但是我在列表框上看到了水平滚动条.

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.

我已经Google搜索并找到了解决类似问题的解决方案,但没有一个起作用.

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>

推荐答案

可以使用属性TextWrapping包装TextBlock的内容. 代替StackPanel,使用DockPanel/Grid. 还有一件事-将ListBoxScrollViewer.HorizontalScrollBarVisibility属性设置为Disabled值.

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.

根据Matt的评论,将Hidden更新为Disabled.谢谢马特.

Updated Hidden to Disabled based on comment from Matt. Thanks Matt.

这篇关于强制将TextBlock包装在WPF ListBox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆