带有一堆项目的 WPF ItemsControl

WPF ItemsControl with one stack of items

我需要在 ItemsControl 中将所有 Items 放在相同的位置 (0, 0),一个堆栈,如 Grid 默认情况下,如果我不设置 Grid.RowGrid.Column

是的,我可以使用 GridCanvas,但也许还有其他更原始的控件?或者可能需要更改一些样式?

我想像往常一样将集合绑定到 ItemsSource,并通过它们的 Margin 属性确定 Items 的位置。我已经完成了偏移逻辑,但在标准 ItemsControl 中,第二个元素的 Margin 是从第一个元素的末尾计算的,我想避免什么。

您可以使用 Grid 作为 ItemsControlItemsPanel:

<ItemsControl>
    <ItemsControl.Items>
        <Rectangle Width="50" Height="50" Fill="Blue" />
        <Rectangle Width="50" Height="50" Fill="Black" />
        <Rectangle Width="50" Height="50" Fill="Yellow" Margin="10,10,0,0" />
    </ItemsControl.Items>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

在上面的示例中,项目按预期相互重叠。