wpf 带有 ItemTemplate(和 ScrollBar!)的 ListBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/483542/
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
ListBox with ItemTemplate (and ScrollBar!)
提问by Pompair
I have a databound and itemtemplated ListBox:
我有一个数据绑定和 itemtemplated ListBox:
<ListBox x:Name="lbLista"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Deleteable, Mode=TwoWay}" />
<Label Content="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The ites show fine and they come from an ObservableCollection.
项目显示正常,它们来自 ObservableCollection。
The problemis the scrollbarwhich appears but is not usable - it does not have a handle to grab. I've tried setting some ScrollView attached properties on ListBox, but they do not affect the situation.
该问题是滚动条出现,但无法使用-它不会有一个把手抢。我试过在 ListBox 上设置一些 ScrollView 附加属性,但它们不影响情况。
采纳答案by Muad'Dib
I pasted your code into test project, added about 20 items and I get usable scroll bars, no problem, and they work as expected. When I only add a couple items (such that scrolling is unnecessary) I get no usable scrollbar. Could this be the case? that you are not adding enough items?
我将您的代码粘贴到测试项目中,添加了大约 20 个项目,我得到了可用的滚动条,没问题,它们按预期工作。当我只添加几个项目(这样滚动是不必要的)时,我没有可用的滚动条。可能是这种情况吗?你没有添加足够的项目?
If you remove the ScrollViewer.VerticalScrollBarVisibility="Visible"
then the scroll bars only appear when you have need of them.
如果您删除 ,ScrollViewer.VerticalScrollBarVisibility="Visible"
则滚动条仅在您需要时才会出现。
回答by Arcturus
ListBox will try to expand in height that is available.. When you set the Height property of ListBox you get a scrollviewer that actually works...
ListBox 将尝试扩展可用的高度。当您设置 ListBox 的 Height 属性时,您会得到一个实际工作的滚动查看器...
If you wish your ListBox to accodate the height available, you might want to try to regulate the Height from your parent controls.. In a Grid for example, setting the Height to Auto in your RowDefinition might do the trick...
如果您希望您的 ListBox 适应可用的高度,您可能想要尝试从您的父控件调节高度。例如,在网格中,在 RowDefinition 中将 Height 设置为 Auto 可能会起作用...
HTH
HTH
回答by EMCS
I have never had any luck with any scrollable content placed inside a stackpanel (anything derived from ScrollableContainer. The stackpanel has an odd layout mechanism that confuses child controls when the measure operation is completed and I found the vertical size ends up infinite, therefore not constrained - so it goes beyond the boundaries of the container and ends up clipped. The scrollbar doesn't show because the control thinks it has all the space in the world when it doesn't.
我从来没有在堆栈面板中放置任何可滚动内容(从 ScrollableContainer 派生的任何内容。堆栈面板有一个奇怪的布局机制,当测量操作完成时会混淆子控件,我发现垂直大小最终是无限的,因此不受约束- 所以它超出了容器的边界并最终被剪裁。滚动条不显示,因为控件认为它拥有世界上的所有空间,而实际上它没有。
You should always place scrollable content inside a container that can resolve to a known height during its layout operation at runtime so that the scrollbars size appropriately. The parent container up in the visual tree must be able to resolve to an actual height, and this happens in the grid if you set the height of the RowDefinition o to auto or fixed.
您应该始终将可滚动内容放置在可以在运行时布局操作期间解析为已知高度的容器内,以便滚动条的大小适当。可视化树中的父容器必须能够解析为实际高度,如果将 RowDefinition o 的高度设置为自动或固定,则会在网格中发生这种情况。
This also happens in Silverlight.
这也发生在 Silverlight 中。
-em-
-em-
回答by Pompair
Thnaks for answer. I tried it myself too to an Empty Project and - lo behold allmighty creator of heaven and seven seas - it worked. I originally had ListBox inside which was inside of root . For some reason ListBox doesn't like being inside of StackPanel, at all! =)
谢谢回答。我自己也尝试过一个空项目 - 瞧,天堂和七海的全能创造者 - 它奏效了。我最初有 ListBox 里面是 root 里面。出于某种原因,ListBox 根本不喜欢在 StackPanel 里面!=)
-pom-
-pom-