如何将画布绑定到矩形列表画布、矩形、绑定、如何将

2023-09-03 01:39:00 作者:执伞青衣袖

使用WPF我已矩形的列表(其可具有在其矩形未定义数量),和一画布。我要使用数据绑定定位在画布上的矩形。

Using WPF I have a list of rectangles (which can have an undefined number of rectangles in it), and a canvas. I want to position those rectangles on the canvas using data binding.

我使用的是项目控制试过了,而且似乎堆栈中的下一个像垂直堆叠面板上的每个项目。

I have tried using an items control, and seems to stack each item on top of the next one like a vertical stack panel.

所有我的矩形具有坐标0,0,但它们都是在彼此向下画布。的顶部

All my rectangles have the co-ordinates 0,0, but they are all on top of each other down the canvas.

要使用项目控制任何替代品?

Any alternatives to using an items control?

推荐答案

您需要的时候必然要考虑的项目包装:

You need to account for the wrapping of the items when bound:

<ItemsControl ItemsSource="{Binding Data}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Canvas.Left" Value="{Binding X}"/>
            <Setter Property="Canvas.Top" Value="{Binding Y}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- Item Template -->
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>