将嵌套的ItemsControl绑定到嵌套的集合

将嵌套的ItemsControl绑定到嵌套的集合

本文介绍了将嵌套的ItemsControl绑定到嵌套的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WPF / Caliburn Micro应用程序中显示页面。页面应以矩形方式呈现给用户。我的想法是对页面使用基本视图模型的集合(行)的集合(行):

I am trying to show pages in my WPF/Caliburn Micro application. The pages should be presented in a rectangular way to the user. My idea is to use a collection (the rows) of collections (the columns) of my base view model for the pages:

public BindableCollection<BindableCollection<BaseViewModel>> Children { get; set; }

然后在关联的视图中执行以下操作:

And do something like this in the associated View:

    <ItemsControl x:Name="Children">
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <ItemsControl ItemsSource="{Binding /}">
            <ItemsPanelTemplate>
              <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
          </ItemsControl>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>

这是错误的-我没有将什么放入内部ItemsControl。

And this is wrong - I don't get what to put into the inner ItemsControl.

感谢任何想法!

我是仍然不确定这是否是完美的解决方案,但是它可以正常工作,而且对我来说似乎不太笨:

I am still not sure whether this is the perfect solution, but it works and seems not too hacky to me:

            <ItemsControl x:Name="Children">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Vertical" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <ContentControl cal:View.Model="{Binding}" />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>


推荐答案

中的正斜杠Binding.Path 表示父集合中的当前项目,但由于它不是集合,因此不会作为 ItemsSource 值:

A forward slash in a Binding.Path means the current item from the parent collection, but that won't as an ItemsSource value because it's not a collection:

<ItemsControl ItemsSource="{Binding /}">   <!-- This won't work  here -->

您还需要定义内部的 ItemsControl.ItemTemplate 。尝试这样的事情:

You also need to define your inner ItemsControl.ItemTemplate. Try something like this:

<ItemsControl ItemsSource="{Binding Children}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate> <!-- Bind to the whole data item (a collection) here -->
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate> <!-- Display your BaseViewModel data items here -->
                        <YourXmlNamespacePrefix:YourControl DataContext="{Binding}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这篇关于将嵌套的ItemsControl绑定到嵌套的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:18