Silverlight-其他ListBox下的ListBox-数据绑定问题

本文关键字:数据绑定 问题 ListBox- 下的 其他 ListBox Silverlight- | 更新日期: 2023-09-27 17:57:33

我有一个ListBox,它包含所有类别并且每个类别将在另一个ListBox中包含子类别如何进行数据绑定?

每个包含"Board"作为其子类别的"Category"这是我的xaml

<ListBox Background="Transparent" BorderThickness="0" Grid.Row="3" Grid.ColumnSpan="2" Margin="0" Padding="0" HorizontalContentAlignment="Stretch" ItemContainerStyle="{StaticResource ListBoxItemStyle}" ItemsSource="{StaticResource designTimeCategoriesDS}" DataContext="{StaticResource designTimeCategoriesDS}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="Category" Orientation="Vertical" HorizontalAlignment="Stretch">
                <Border CornerRadius="6" Padding="0" Margin="0" Height="30" HorizontalAlignment="Stretch">
                    <Border.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF6D869F" Offset="0"/>
                            <GradientStop Color="#FFA6BACE" Offset="1"/>
                        </LinearGradientBrush>
                    </Border.Background>
                    <sdk:Label x:Name="CategoryName" FontSize="16" Margin="8,0" Foreground="White" FontWeight="Bold" Height="30" HorizontalAlignment="Stretch"/>
                </Border>
                <ListBox x:Name="CategoryBoards" Background="Transparent" BorderThickness="0" Margin="0" Padding="0" HorizontalContentAlignment="Stretch" ItemContainerStyle="{StaticResource ListBoxItemStyle}" ItemsSource="{Binding }">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="0" HorizontalAlignment="Stretch">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="70"/>                  
                                    <ColumnDefinition Width="350*"/>                
                                    <ColumnDefinition Width="70*"/>                 
                                    <ColumnDefinition Width="70*"/>                 
                                </Grid.ColumnDefinitions>
                                <HyperlinkButton FontSize="16" Margin="0,0,0,1" Padding="8" Foreground="Black" FontWeight="Bold" VerticalAlignment="Stretch" Background="#FFE7EAEF"></HyperlinkButton>
                                <StackPanel Grid.Column="1" Margin="1,0,0,1" Background="#FFF0F4F7">
                                    <HyperlinkButton FontSize="14" Margin="0" Padding="8,8,8,0" Foreground="#FFD97B33" FontWeight="Bold" Content="{Binding Path=CategoryBoards.BoardName}" />
                                    <sdk:Label Margin="0" Padding="8,0,8,8" Foreground="Black" Content="{Binding Path=CategoryBoards.BoardDescription}" />
                                </StackPanel>
                                <StackPanel Grid.Column="2" Margin="1,0,0,1" Background="#FFE7EAEF">
                                    <sdk:Label Margin="0" Padding="8,8,8,0" Content="{Binding Path=BoardPosts}" />
                                    <sdk:Label Margin="1,0,0,1"  Padding="8,0,8,8" Content="{Binding Path=BoardTopics}" />
                                </StackPanel>
                                <StackPanel Grid.Column="3" Margin="1" Background="#FFF0F4F7">
                                    <sdk:Label Margin="0" Padding="4,2,4,0" Content="Last post by {User}"/>
                                    <sdk:Label Margin="0"  Padding="4,0,4,0" Content="in {Topic Name}"/>
                                    <sdk:Label Margin="0"  Padding="4,0,4,2" Content="on {Date}"/>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>                                          
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Silverlight-其他ListBox下的ListBox-数据绑定问题

只要快速浏览一下您的代码,您唯一需要做的就是更改这一行:

<ListBox x:Name="CategoryBoards" Background="Transparent" BorderThickness="0" 
         Margin="0" Padding="0" HorizontalContentAlignment="Stretch" 
         ItemContainerStyle="{StaticResource ListBoxItemStyle}" 
         ItemsSource="{Binding }">

到此:

<ListBox x:Name="CategoryBoards" Background="Transparent" BorderThickness="0" 
         Margin="0" Padding="0" HorizontalContentAlignment="Stretch" 
         ItemContainerStyle="{StaticResource ListBoxItemStyle}" 
         ItemsSource="{Binding Boards}">

内部ListBox的DataContext将是Category,因此该绑定的源隐含地是Category。