如何将画布绑定到矩形列表 [英] How to bind a canvas to a list of rectangles

查看:129
本文介绍了如何将画布绑定到矩形列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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>

这篇关于如何将画布绑定到矩形列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆