2012-12-23 116 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") 

希望對您有所幫助..