绑定一个嵌套表到WPF画布面板 [英] Binding a nested list to a WPF canvas panel

查看:153
本文介绍了绑定一个嵌套表到WPF画布面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果可能的列表清单以某种方式绑定到卡瓦酒面板。例如具有对象层,它本身是矩形的对象的列表。假若有可能图层列表绑定到印刷品吗?到目前为止,我只能通过(使用的SelectMany功能)结合嵌套列表的扁平INumerable到itemcontrol做到这一点,但是这不是足够好,我还想保留层分开,并有矩形的zIndex的变化,根据该层是在,允许轻松地重新排序层。



我也试过有嵌套itemcontrols但预期它只能显示第一层。这样做的目的是要绘制的每个目标的矩形的每一层的画布允许层处理,在每一层的新对象的插入,在等...



由于上提前! :)


解决方案

如何使主ItemsControl的另一个的的ItemTemplate ItemsControl的又一帆布为 ItemsPanel ? (我不明白了一个道理,为什么它应该只能显示第一层)



其实主要的ItemsControl并不需要是一个画布,网格会做的一样好(至少,除非你收藏有自己或Z顺序的坐标,如果您使用网格图层顺序是,他同他们集合在外观上)。



一个例子(有点冗长,但不能做任何事情):



<预类=郎咸平的XML prettyprint-覆盖> <的ItemsControl的ItemsSource ={绑定数据}>
< ItemsControl.ItemsPanel>
< ItemsPanelTemplate>
<网/>
< / ItemsPanelTemplate>
< /ItemsControl.ItemsPanel>
< ItemsControl.ItemTemplate>
<&DataTemplate的GT;
<的ItemsControl的ItemsSource ={结合LayerItems}>
< ItemsControl.ItemsPanel>
< ItemsPanelTemplate>
<帆布/>
< / ItemsPanelTemplate>
< /ItemsControl.ItemsPanel>
< ItemsControl.ItemContainerStyle>
<风格的TargetType ={X:类型ContentPresenter}>
<! - 这里将结合一些属性需要安置的护理 - >
< setter属性=Canvas.TopVALUE ={结合顶部}/>
< setter属性=Canvas.LeftVALUE ={结合左}/>
< /样式和GT;
< /ItemsControl.ItemContainerStyle>
< ItemsControl.ItemTemplate>
<! - 一些模板个别项目 - >
<&DataTemplate的GT;
< BORDER填充=10了borderThickness =1BorderBrush =红CornerRadius =3
=背景白>
< TextBlock的文本={绑定名称}/>
< /边框>
< / DataTemplate中>
< /ItemsControl.ItemTemplate>
< / ItemsControl的>
< / DataTemplate中>
< /ItemsControl.ItemTemplate>
< / ItemsControl的>


I was wondering if its possible to somehow bind a list of lists to a cavas panel. For example having object "layer" which itself is a list of "rectangle" objects. Would if be possible to bind a list of layers to a canvas? So far I can only do it by binding a flattened INumerable of the nested list (using SelectMany function) to an itemcontrol, but this isnt good enough, Id like to keep the "layers" separate and have the Zindex of the rectangles vary according to the layer it is at, allowing to reorder the layers easily.

I have also tried having nested itemcontrols but as expected it only displays the first layer. The aim is to draw a rectangle for every object in every layer on the canvas allowing for layer manipulation, insertion of new objects in each layer, etc...

Thanks in advance! :)

解决方案

How about making the ItemTemplate of the main ItemsControl another ItemsControl with yet another Canvas as ItemsPanel? (I do not see a reason why it should only display the first layer)

Actually the main ItemsControl does not need to be a Canvas, a Grid would do as well (at least unless your collections have coordinates of their own or a Z-Order, if you use a grid the layer order is he same as their appearance in the collection).

An example (a bit verbose but can't do anything about that):

<ItemsControl ItemsSource="{Binding Data}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding LayerItems}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="{x:Type ContentPresenter}">
                        <!-- Here would the binding to some properties that take care of placement -->
                        <Setter Property="Canvas.Top" Value="{Binding Top}" />
                        <Setter Property="Canvas.Left" Value="{Binding Left}" />
                    </Style>
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemTemplate>
                    <!-- Some template for the individual items -->
                    <DataTemplate>
                        <Border Padding="5" BorderThickness="1" BorderBrush="Red" CornerRadius="3"
                                Background="White">
                            <TextBlock Text="{Binding Name}" />
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这篇关于绑定一个嵌套表到WPF画布面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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