我有一个ItemsControl,我计划在其中托管一个水果对象列表。我有一个Fruit对象列表,每个对象都有自己的DataTemplate。我有一个Grape对象,一个Orange对象和一个Kiwi对象。
我希望使用UniformGrid使所有单元格具有相同的大小,但我希望Kiwi对象仅跨越1个单元格,我希望Grape跨越2个单元格(ColumnSpan = 2),我希望Orange控件跨越4个单元格(ColumnSpan = 2和RowSpan = 2)
我如何才能做到这一点呢?
发布于 2012-06-13 00:05:17
UniformGrid中的项不能跨多个单元格。
为什么不使用将行/列的高度/宽度设置为*的常规Grid,以便它们的大小相等?如果您希望单元格成为高度与宽度匹配的完美正方形,请确保将网格的Height绑定到Width,反之亦然。
需要注意的是,您需要在ItemContainerStyle中应用网格定位属性,而不是应用于项目本身,因为在将项目添加到ContentPresenter之前,ItemsControl会将每个元素包装在ItemsPanel中(有关详细信息,请参阅我的博客文章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>发布于 2012-06-12 23:45:05
您不能在统一网格中进行行或列跨越-请参阅下面的stackoverflow问题
WPF: Is it possible to do a row/column span in UniformGrid?
但是,如果您的项目具有统一的尺寸,则您可以使用WrapPanel实现类似的效果。这里有一个例子
<ItemsControl Width="300">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Items>
<Rectangle Fill="Orange" Height="100" Width="100" />
<Rectangle Fill="Red" Height="100" Width="100" />
<Rectangle Fill="Blue" Height="100" Width="100" />
<Rectangle Fill="Green" Height="100" Width="200" />
<Rectangle Fill="Yellow" Height="100" Width="100" />
<Rectangle Fill="Brown" Height="100" Width="100" />
<Rectangle Fill="Black" Height="100" Width="200" />
</ItemsControl.Items>
</ItemsControl>https://stackoverflow.com/questions/10999715
复制相似问题