将嵌套的ListBox绑定到嵌套的List?

时间:2011-11-28 14:14:40

标签: wpf listbox itemtemplate

我有以下ViewModel-Property:

public List<List<string>> Names .... //It's a dependency property

在我看来,我希望ItemsControl有一个ItemsControl

 <ItemsControl ItemsSource="{Binding Names}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ItemsControl ItemsSource="{Binding ?????}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Label Text="{Binding ??????}" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

如何绑定List的项目?在上面的代码示例中,我用?????

标记了它

1 个答案:

答案 0 :(得分:6)

只需使用绑定到当前绑定源:

ItemsSource="{Binding}"

请参阅以下评论:

<ItemsControl ItemsSource="{Binding Names}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <! -- Here is the current binding source will be inner
                      List<string> 
                -->
                <ItemsControl ItemsSource="{Binding}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>

                    <! -- Here is the current binding wource will be 
                          a string value from the inner List<string>
                    -->
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Label Text="{Binding}" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>