【问题标题】:Binding nested ItemsControls to nested collections将嵌套的 ItemsControls 绑定到嵌套的集合
【发布时间】:2014-02-05 16:34:59
【问题描述】:

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

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

并在关联的视图中执行类似的操作:

    <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 的内容。

感谢您的任何想法!

解决方案

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

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

【问题讨论】:

    标签: wpf caliburn.micro


    【解决方案1】:

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

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

    你还需要定义你内心的ItemsControl.ItemTemplate。试试这样的:

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

    【讨论】:

    • 谢谢!虽然这并没有为我的问题提供直接的解决方案(我认为它与 Caliburn Micro 的细节有关),但它给了我解决它的正确想法。我会相应地更新问题。
    猜你喜欢
    • 1970-01-01
    • 2016-04-20
    • 2022-06-11
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多