2012-12-23 84 views
1
<Grid x:Name="ContentPanel3" Grid.Row="1" Margin="12,0,12,0"> 
    <ScrollViewer> 
    <StackPanel> 
     <ListBox Height="500" Padding="2" Name="listBox1" ItemsSource="{Binding School}" Width="460"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
      <StackPanel> 
       <TextBlock Name="elementtype" Text="{Binding type}"/> 
       <ListBox x:Name="underlist" ItemsSource="{Binding listschoolclass}" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
        <StackPanel> 
         <TextBlock Name="elementssalle" Text="{Binding room}"/> 
         <TextBlock Name="elementsdebut" Text="{Binding teacher}"/> 
        </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
       </ListBox> 
      </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 
    </ScrollViewer> 
</Grid> 

这是我的问题:例如listBox1.ItemSource = ...:在xaml.cs我可以访问到元素listBox1用这种方法。但是我无法到达嵌套列表框的元素下标列表。如何访问到嵌套列表框

回答

0

您可以定义资源的内衬像

<Window.Resources> 
    <ListBox x:Key="underlist" ItemsSource="{Binding listschoolclass}" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Name="elementssalle" Text="{Binding room}"/> 
        <TextBlock Name="elementsdebut" Text="{Binding teacher}"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Window.Resources> 

,然后你主要的XAML会像

<Grid x:Name="ContentPanel3" Grid.Row="1" Margin="12,0,12,0"> 
    <ScrollViewer> 
     <StackPanel> 
      <ListBox Height="500" Padding="2" Name="listBox1" ItemsSource="{Binding School}" Width="460"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Name="elementtype" Text="{Binding type}"/> 
          <ContentControl Content="{StaticResource underlist}"/> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </StackPanel> 
    </ScrollViewer> 
</Grid> 
中的.cs

文件,您可以通过访问该资源

this.FindResource("underList") 

希望对您有所帮助..