1

ItemsControl果物のオブジェクトのリストをホストする予定のがあります。Fruitすべて独自のオブジェクトを持つオブジェクトのリストがありますDataTemplate。私はGrapeオブジェクト、オブジェクト、オブジェクトOrangeを持っていKiwiます。

UniformGridすべてのセルが同じサイズになるようにaを使用したいのですが、Kiwiオブジェクトを1つのセルだけにまたがらせ、Grape2つのセル( )にまたがらせ、コントロールを4つのセル(および)にまたがらColumnSpan = 2せたいOrangeColumnSpan = 2RowSpan = 2

どうすればこれを達成できますか?

4

2 に答える 2

5

内の項目は、UniformGrid複数のセルにまたがることはできません。

行/列の高さ/幅がすべて同じサイズになるようにGrid、代わりに通常の行/列を使用しないのはなぜですか? *セルを幅と高さが一致する完全な正方形にしたい場合は、グリッドを にバインドするHeightWidth、その逆を行ってください。

アイテムを に追加する前に が各要素を 内にラップするため、グリッド配置プロパティをItemContainerStyleアイテム自体ではなくに適用する必要があることに注意してください(詳細については、こちらのブログ投稿を参照してください)。ItemsControlContentPresenterItemsPanel

<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-12T16:05:16.507 に答える
0

均一なグリッドで行または列のスパンを行うことはできません-以下のstackoverflowの質問を参照してください

WPF: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>
于 2012-06-12T15:45:04.567 に答える