如何在 ListBox 中获得垂直滚动条? [英] How can I get a vertical scrollbar in my ListBox?
问题描述
在下面的示例中,我有一个 ListBox,其中包含许多字体名称.
In the example below I have a ListBox with dozens of font names in it.
我原以为它会自动有一个垂直滚动条,这样你就可以选择任何字体,而不仅仅是列表中的第一个字体,但事实并非如此.
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.
所以我添加了一个ScrollViewer",并在右侧放置了一个滚动条区域",但滚动条区域中没有滚动条,因此您可以滚动(!).
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 (!).
为什么滚动条不是自动的,我如何强制它有滚动条?
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>
推荐答案
您的解决方案的问题是您在 ListBox 周围放置了一个滚动条,您可能希望将它放在 ListBox 中.
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 中强制使用滚动条,请使用 ScrollBar.VerticalScrollBarVisibility 附加属性.
If you want to force a scrollbar in your ListBox, use the ScrollBar.VerticalScrollBarVisibility attached property.
<ListBox
ItemsSource="{Binding}"
ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>
将此值设置为 Auto 将根据需要弹出滚动条.
Setting this value to Auto will popup the scrollbar on an as needed basis.
这篇关于如何在 ListBox 中获得垂直滚动条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!