扩展器中的滚动查看器 [英] ScrollViewer in Expander
问题描述
我有一个带有扩展器的列表框,它又包含一个列表框.我想让 ListBox 和 Expander (listBox1) 和每个 Expander 中的 ListBox (listBox2) 都具有滚动功能,但我无法让最里面的滚动工作(即我的 XAML 中的 scrollViewer1).
I have a ListBox with Expanders which in turn contains a ListBox. I would like to have both the ListBox with Expanders (listBox1) and the ListBox inside each Expander (listBox2) to have scroll functionality, but I cannot get the innermost scrolling to work (i.e. scrollViewer1 in my XAML).
如何让两个滚动条都工作?
How can I get both scrollbars to work?
<ScrollViewer x:Name="scrollViewer1">
<ListBox x:Name="listBox1" ItemsSource="{Binding Data}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Expander>
<Expander.Header>
<TextBlock Text="{Binding Name}">
</TextBlock>
</Expander.Header>
<ScrollViewer x:Name="scrollViewer2">
<ListBox x:Name="listBox2" ItemsSource="{Binding Numbers}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Grid>
<TextBlock Grid.Column="0" Text="{Binding}"/>
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Expander>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
推荐答案
首先,您不需要 scrollviewer 标签:列表框自己处理滚动.要控制列表框滚动条的可见性,您可以使用 ScrollViewer.HorizontalScrollBarVisibility
和 ScrollViewer.VerticalScrollBarVisibility
.
First of all, you d'ont need the scrollviewer tags: The listbox handles the scrolling by itself. To control the visibilty of the listbox scrollbar you ca use the ScrollViewer.HorizontalScrollBarVisibility
and ScrollViewer.VerticalScrollBarVisibility
.
其次,内部列表框的垂直滚动条不会出现,因为没有高度限制,列表框会展开以显示所有项目.
Second, the vertical scrollbar of the inner listbox does not appear because without height limitations the listbox will expand to display all the items.
这是一个工作代码,它显示了父列表框和内部列表框的滚动条:
Here is a working code wich shows both scroll bar of parent and inner listbox :
<Window x:Class="StackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox x:Name="listBox1" ItemsSource="{Binding Clients}">
<ListBox.ItemTemplate>
<DataTemplate>
<Expander>
<Expander.Header>
<TextBlock Text="{Binding Name}">
</TextBlock>
</Expander.Header>
<ListBox x:Name="listBox2" ItemsSource="{Binding Children}" MaxHeight="150">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Grid>
<TextBlock Grid.Column="0" Text="{Binding Name}"/>
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Expander>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
要重现您的问题,只需删除第二个列表框上的 MaxHeight.
To reproduce your problem, just remove the MaxHeight on the second listbox.
这篇关于扩展器中的滚动查看器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!