[英]ScrollViewer in Expander
我有一個帶有擴展器的ListBox,后者又包含一個ListBox。 我想同時使用帶有擴展器的ListBox(listBox1)和每個擴展器中的ListBox(listBox2)具有滾動功能,但是我無法使最里面的滾動工作(例如,XAML中的scrollViewer1)。
如何使兩個滾動條都能工作?
<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
。
其次,內部列表框的垂直滾動條不會出現,因為沒有高度限制,列表框將展開以顯示所有項目。
這是一個工作代碼,它同時顯示了父級和內部列表框的滾動條:
<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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.