如何在ListBox中获得垂直滚动条? [英] How can I get a vertical scrollbar in my 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.
如果要在列表框中强制使用滚动条,请使用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>
将此值设置为自动"将根据需要弹出滚动条.
Setting this value to Auto will popup the scrollbar on an as needed basis.
这篇关于如何在ListBox中获得垂直滚动条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!