简体   繁体   English

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

[英]Silverlight - ListBox under an other ListBox - Databinding Question

I have a ListBox which contains all the Categories and each category will contain subcategories in an other ListBox how do i databind?我有一个包含所有类别的列表框,每个类别将包含另一个列表框中的子类别我如何进行数据绑定?

each "Category" containing "Boards" as its subcategory this is my xaml每个包含“Boards”作为子类别的“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>

Just glancing at your code quickly, the only thing you should need to do is change this line:只需快速浏览一下您的代码,您唯一需要做的就是更改这一行:

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

to this:对此:

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

The DataContext of your inner ListBox will be the Category, so the source of that binding is, implicitly, the Category.您的内部 ListBox 的 DataContext 将是 Category,因此该绑定的源隐含地是 Category。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM