2015-09-18 20 views
0

我有一个带有扩展器的ListBox,它又包含一个ListBox。 我希望ListBox和Expanders(listBox1)以及每个Expander(listBox2)中的ListBox都具有滚动功能,但我无法让最内层的滚动工作(即我的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> 
一切,你闯到大的

回答

0

首先需要的ScrollViewer标签:列表框本身处理滚动。要控制列表框滚动条的可见性,您可以使用ScrollViewer.HorizontalScrollBarVisibilityScrollViewer.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。

+0

谢谢!当窗口调整大小时,如何让listBox2将其高度调整为适合窗口的大小? – aengas