项目在一个UniformGrid
不能跨越多个细胞。
为什么不使用常规Grid
而是将行/列的高度/宽度设置为*
,因此它们的大小都是相同的?如果希望单元格是高度与宽度匹配的完美正方形,请确保将网格的Height
绑定到Width
,反之亦然。
应当注意的是,你需要在ItemContainerStyle
每个元素加入该项目的ItemsPanel
前应用网格定位性能,而不是项目本身,因为一个ItemsControl
的身体包裹ContentPresenter
内(见我的博客文章here为更多详情)
<ItemsControl ItemsSource="{Binding MyCollection}">
<!-- ItemsPanelTemplate -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!-- ItemContainerStyle -->
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Column"
Value="{Binding ColumnIndex}" />
<Setter Property="Grid.Row"
Value="{Binding RowIndex}" />
<!-- Can either use a DataTrigger to determine these values,
or store them on the object itself -->
<Setter Property="Grid.RowSpan"
Value="{Binding RowSpan}" />
<Setter Property="Grid.ColumnSpan"
Value="{Binding ColumnSpan}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>