WPF 列表框不会垂直滚动
在 Groupbox 中,我有一个 Listbox,ListboxItems 也在 XAML 中定义。列表框的定义如下:
<ListBox Name="lvAvoidCountry" Margin="5,5,5,5"
Background="Transparent"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
项目的定义如下:
<ListViewItem >
<CheckBox Name="chkAlbanien" Tag="55">
<StackPanel Orientation="Horizontal">
<Image Source="images/flag_albania.png" Height="30"></Image>
<TextBlock Text="Albanien" Margin="5,0,0,0"></TextBlock>
</StackPanel>
</CheckBox>
</ListViewItem>
如果我删除 Scrollviewer 设置,我会得到水平滚动,并且项目的格式良好 - 宽度正确。如果我使用滚动查看器设置,项目会被切断,以便所有项目都放置在列表框中。 (例如,显示了标志,显示了复选框,但文本只是“Alba”)。
感谢您的任何提示!
Within a Groupbox I have a Listbox, ListboxItems are defined in the XAML as well. The Listbox is defined:
<ListBox Name="lvAvoidCountry" Margin="5,5,5,5"
Background="Transparent"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
Items are defined like this:
<ListViewItem >
<CheckBox Name="chkAlbanien" Tag="55">
<StackPanel Orientation="Horizontal">
<Image Source="images/flag_albania.png" Height="30"></Image>
<TextBlock Text="Albanien" Margin="5,0,0,0"></TextBlock>
</StackPanel>
</CheckBox>
</ListViewItem>
If I remove the Scrollviewer Settings I get horizontal scrolling and the Items are well formatted - correct width. If I use the scrollviewer settings the items get cut off so that all items are placed on the listbox. (eg. the flag is shown, the checkbox is shown but the text is just "Alba").
Thanks for any hints!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
顾名思义,
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
禁用水平滚动。如果您这样做,但您的 ListBoxItems 太长,它们将被截断。 StackPanel 不会增大或缩小以适合 ListBox,并且如果列表框太窄,它也不会“包裹”您的项目以适合 ListBox,即使您将TextWrapping
添加到 TextBlock 也是如此。它非常顽固。我认为你的主要问题是 StackPanel。尝试使用具有 2 列定义的 Grid,而不是 StackPanel,如下所示:
Auto
将“收缩包裹”图像列,而*
将为文本提供所有剩余空间。然后将TextWrapping
添加到文本块中,以防它仍然太长。编辑:添加了更完整的代码示例并稍微更改了我的答案。
As the name implies,
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
disables horizontal scrolling. If you do that, but your ListBoxItems are too long, they'll get cut off. The StackPanel won't grow or shrink to fit into the ListBox, and it won't "wrap" your items to fit into the ListBox if it's too narrow, even if you addTextWrapping
to the TextBlock. It's very stubborn. I think your main problem is that StackPanel.Instead of a StackPanel, try using a Grid with 2 columns defined like so:
Auto
will "shrinkwrap" the image columns, and*
will give the text all remaining space. Then addTextWrapping
to your textblock in case it's still too long.Edited: added more complete code example and changed my answer slightly.
如果您想在列表框中垂直滚动,那么不要将其放入堆栈面板中,而是使用网格。
if you want vertical scrolling in a listbox then don't put it in a stackpanel,instead use a grid.