在我的 ViewModel 中,我有一个项目列表,我希望视图中的网格绑定到这些项目(这些项目将是网格子项)。该列表是项目的视图模型列表。
如何将网格绑定到列表(我可以在代码中访问 .children,但不能访问 xaml)?此外,如何为列表中的视图模型指定数据模板(另一个 xaml 文件),以便它们在网格中正确呈现。
谢谢
将 anItemsControl
与ItemsPanel
集合一起使用为 Grid :
<ItemsControl ItemsSource="{Binding TheList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
在ItemsControl
'sItemContainerStyle
中,您可能希望将 theGrid.Row
和Grid.Column
附加属性绑定到 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>