如何访问嵌套列表框

时间:2012-12-23 17:52:26

标签: c# wpf listbox nested windows-phone-7.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>

这是我的问题:在xaml.cs中,我可以使用此方法访问元素listBox1:例如listBox1.ItemSource = ...。但是我无法访问嵌套列表框的元素下载列表。

1 个答案:

答案 0 :(得分: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")
希望它有所帮助..