如何将网格的子项绑定到列表? [英] How do you bind a grid's children to a list?

查看:17
本文介绍了如何将网格的子项绑定到列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 ViewModel 中,我有一个项目列表,我希望视图中的网格绑定到这些项目(这些项目将是网格子项).该列表是项目的视图模型列表.

In my ViewModel I have a list of items that I would like a grid in my view to bind to (the items will be the grids children). The list is a list of view models for the items.

如何将网格绑定到列表(我可以在代码中访问 .children,但不能访问 xaml)?另外,如何为列表中的视图模型指定数据模板(另一个 xaml 文件),以便它们在网格中正确呈现.

How do you bind a grid to the list (I can access .children in code but not xaml)? Also, how do you specify the data template (another xaml file) for the view models in the list so that they are rendered correctly within the grid.

谢谢

推荐答案

使用 ItemsControl 并将 ItemsPanel 设置为 Grid:

Use an ItemsControl with the ItemsPanel set to a Grid :

<ItemsControl ItemsSource="{Binding TheList}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <Grid/>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

ItemsControlItemContainerStyle 中,您可能想要绑定 Grid.RowGrid.Column 将属性附加到项目的某些属性:

In the ItemsControl's ItemContainerStyle, you might want to bind the Grid.Row and Grid.Column attached properties to some property of the items :

  <ItemsControl.ItemContainerStyle>
    <Style TargetType="{x:Type FrameworkElement}">
        <Setter Property="Grid.Row" Value="{Binding RowIndex}"/>
        <Setter Property="Grid.Column" Value="{Binding ColumnIndex}"/>
    </Style>
  </ItemsControl.ItemContainerStyle>

这篇关于如何将网格的子项绑定到列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆