wpf 如何在我的 ListBox 中获得垂直滚动条?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/472796/
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
How can I get a vertical scrollbar in my ListBox?
提问by Edward Tanguay
In the example below I have a ListBox with dozens of font names in it.
在下面的示例中,我有一个 ListBox,其中包含许多字体名称。
I would have thought it would automatically have a vertical scrollbar on it so that you can select ANY font, not just the first ones in the list, but it doesn't.
我原以为它会自动有一个垂直滚动条,以便您可以选择任何字体,而不仅仅是列表中的第一个字体,但事实并非如此。
So I added a "ScrollViewer" and that puts a "scrollbar area" on the right but there is no scrollbar in the scrollbar area so that you can scroll (!).
因此,我添加了一个“ScrollViewer”,并在右侧放置了一个“滚动条区域”,但滚动条区域中没有滚动条,因此您可以滚动(!)。
Why isn't a scrollbar automatic and how do I force it to have a scrollbar?
为什么滚动条不是自动的,我如何强制它有滚动条?
<StackPanel Name="stack1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<ScrollViewer>
<ListBox Grid.Row="0" Name="lstFonts" Margin="3" ItemsSource="{x:Static Fonts.SystemFontFamilies}"/>
</ScrollViewer>
</Grid>
</StackPanel>
回答by JaredPar
The problem with your solution is you're putting a scrollbar around a ListBox where you probably want to put it inside the ListBox.
您的解决方案的问题是您在 ListBox 周围放置了一个滚动条,您可能希望将它放在 ListBox 中。
If you want to force a scrollbar in your ListBox, use the ScrollBar.VerticalScrollBarVisibility attached property.
如果要在 ListBox 中强制使用滚动条,请使用 ScrollBar.VerticalScrollBarVisibility 附加属性。
<ListBox
ItemsSource="{Binding}"
ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>
Setting this value to Auto will popup the scrollbar on an as needed basis.
将此值设置为 Auto 将根据需要弹出滚动条。
回答by yossharel
ListBox
already contains ScrollViewer
. By default the ScrollBar
will show up when there is more content than space. But some containers resize themselves to accommodate their contents (e.g. StackPanel
), so there is never "more content than space". In such cases, the ListBox
is always given as much space as is needed for the content.
ListBox
已经包含ScrollViewer
. 默认情况下ScrollBar
,当内容多于空间时会显示。但是有些容器会调整自己的大小以容纳它们的内容(例如StackPanel
),因此永远不会有“内容多于空间”。在这种情况下,ListBox
总是为内容提供尽可能多的空间。
In order to calculate the condition of having more content than space, the size should be known. Make sure your ListBox
has a constrained size, either by setting the size explicitly on the ListBox
element itself, or from the host panel.
为了计算内容多于空间的条件,大小应该是已知的。ListBox
通过在ListBox
元素本身或主机面板上明确设置大小,确保您的大小受到限制。
In case the host panel is vertical StackPanel
and you want VerticalScrollBar
you must set the Height on ListBox
itself. For other types of containers, e.g. Grid
, the ListBox
can be constrained by the container. For example, you can change your original code to look like this:
如果主机面板是垂直的StackPanel
并且您希望VerticalScrollBar
您必须在ListBox
其自身上设置高度。对于其他类型的容器,例如Grid
,ListBox
可以受到容器的约束。例如,您可以将原始代码更改为如下所示:
<Grid Name="grid1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<ListBox Grid.Row="0" Name="lstFonts" Margin="3"
ItemsSource="{x:Static Fonts.SystemFontFamilies}"/>
</Grid>
</Grid>
Note that it is not just the immediate container that is important. In your example, the immediate container is a Grid
, but because that Grid
is contained by a StackPanel
, the outer StackPanel
is expanded to accommodate its immediate child Grid
, such that that child can expand to accommodate itschild (the ListBox
).
请注意,重要的不仅仅是直接容器。在您的示例中,直接容器是 a Grid
,但由于它Grid
包含在 a 中StackPanel
,因此外部容器StackPanel
被扩展以容纳其直接子容器,以便该子容器Grid
可以扩展以容纳其子容器(the ListBox
)。
If you constrain the height at any point — by setting the height of the ListBox
, by setting the height of the inner Grid
, or simply by making the outer container a Grid
— then a vertical scroll bar will appear automatically any time there are too many list items to fit in the control.
如果你在任何一点限制高度——通过设置 的高度ListBox
,通过设置内部的高度Grid
,或者简单地将外部容器设置为Grid
a——那么当列表项太多时,垂直滚动条会自动出现适合控制。
回答by Edward Tanguay
I added a "Height" to my ListBox and it added the scrollbar nicely.
我在列表框中添加了一个“高度”,它很好地添加了滚动条。
回答by Paras
Scroll Bar is added to the List box automatically unless its visibility is set to Hidden. Whenever the size of List Items exceeds the one, which can be shown inside a list box vertical or horizontal list box can be seen during the run time.
滚动条会自动添加到列表框,除非其可见性设置为隐藏。每当 List Items 的大小超过 1,它可以显示在一个列表框内,在运行时可以看到垂直或水平列表框。
回答by oldDavid
In my case the number of items in the ListBox is dynamic so I didn't want to use the Height property. I used MaxHeight instead and it works nicely. The scrollbar appears when it fills the space I've allocated for it.
在我的例子中,ListBox 中的项目数是动态的,所以我不想使用 Height 属性。我改用 MaxHeight,效果很好。滚动条在填满我为其分配的空间时出现。
回答by Bill Clyde
I was having the same problem, I had a ComboBox followed by a ListBox in a StackPanel and the scroll bar for the ListBox was not showing up. I solved this by putting the two in a DockPanel instead. I set the ComboBox DockPanel.Dock="Top" and let the ListBox fill the remaining space.
我遇到了同样的问题,我在 StackPanel 中有一个 ComboBox,后跟一个 ListBox,但 ListBox 的滚动条没有显示。我通过将两者放在 DockPanel 中解决了这个问题。我设置了 ComboBox DockPanel.Dock="Top" 并让 ListBox 填充剩余空间。
回答by Mohammad Fazeli
XAML ListBox Scroller - Windows 10(UWP)
XAML 列表框滚动条 - Windows 10(UWP)
<Style TargetType="ListBox">
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Visible"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Visible"/>
</Style>