是否有与 ItemsControl.ItemTemplate
与 Grid
配合使用的等效机制?我有一个项目集合,我想将它们呈现为 Grid 中的行,以便我可以将 Grid.Column 分配给模板内的各个元素(如与列表控件中的行相反)。在使用标准控件的 WPF 中这可能吗?
最佳答案
好的,使用带有 Grid.IsSharedSizeScope="true"
的 ItemsControl应用附加属性(property)。接下来,对于您的 ItemTemplate,您使用 <Grid>
就像您通常所做的那样,除了现在添加 ColumnDefinition
时您设置 SharedSizeGroup
属性为每列唯一的名称。例如:
<ItemsControl Grid.IsSharedSizeScope="true">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="MyFirstColumn" />
<ColumnDefinition SharedSizeGroup="MySecondColumn" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding MyFirstProperty}"/ >
<TextBlock Grid.Column="1" Text="{Binding MySecondProperty}"/ >
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
有关 IsSharedSizeScope 和 SharedSizeGroup 的更多信息,check out this section of the SDK 。应该注意的是,RowDefinitions 也有一个 SharedSizeGroup,以便您也可以进行水平布局。
关于.net - WPF - 没有容器的数据模板,项目作为网格中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1639948/