Silverlight - 另一个ListBox下的ListBox - 数据绑定问题

时间:2011-05-23 21:25:13

标签: c# silverlight data-binding listbox nested

我有一个包含所有类别的ListBox 每个类别将包含另一个ListBox中的子类别 我怎么做数据绑定?

每个“类别”包含“Boards”作为其子类别 这是我的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>

1 个答案:

答案 0 :(得分:1)

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

<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。