首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF UniformGrid布局

WPF UniformGrid布局
EN

Stack Overflow用户
提问于 2012-06-12 23:20:04
回答 2查看 9.5K关注 0票数 1

我有一个ItemsControl,我计划在其中托管一个水果对象列表。我有一个Fruit对象列表,每个对象都有自己的DataTemplate。我有一个Grape对象,一个Orange对象和一个Kiwi对象。

我希望使用UniformGrid使所有单元格具有相同的大小,但我希望Kiwi对象仅跨越1个单元格,我希望Grape跨越2个单元格(ColumnSpan = 2),我希望Orange控件跨越4个单元格(ColumnSpan = 2RowSpan = 2)

我如何才能做到这一点呢?

EN

回答 2

Stack Overflow用户

发布于 2012-06-13 00:05:17

UniformGrid中的项不能跨多个单元格。

为什么不使用将行/列的高度/宽度设置为*的常规Grid,以便它们的大小相等?如果您希望单元格成为高度与宽度匹配的完美正方形,请确保将网格的Height绑定到Width,反之亦然。

需要注意的是,您需要在ItemContainerStyle中应用网格定位属性,而不是应用于项目本身,因为在将项目添加到ContentPresenter之前,ItemsControl会将每个元素包装在ItemsPanel中(有关详细信息,请参阅我的博客文章here )

代码语言:javascript
复制
<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>
票数 5
EN

Stack Overflow用户

发布于 2012-06-12 23:45:05

您不能在统一网格中进行行或列跨越-请参阅下面的stackoverflow问题

WPF: Is it possible to do a row/column span in UniformGrid?

但是,如果您的项目具有统一的尺寸,则您可以使用WrapPanel实现类似的效果。这里有一个例子

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10999715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档