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这是我的问题:在 xaml.cs 中,我可以使用这种方法访问元素:listBox1.ItemSource = ...例如。但我无法到达嵌套列表框的元素下列表。

4

1 回答 1

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")

希望能帮助到你..

于 2012-12-24T02:10:39.773 回答