【发布时间】:2013-07-10 12:16:57
【问题描述】:
我有一个画布,其中包含几个不同的形状,它们都是静态的,并且绑定到视图模型 (MVVM) 中的不同属性。截至目前,画布定义如下(简化):
<Canvas>
<Polygon Fill="Red" Stroke="Gray" StrokeThickness="3" Points="{Binding StorageVertices}" />
<Ellipse Fill="Blue" Width="{Binding NodeWidth}" Height="{Binding NodeHeight}" />
<!-- And some more static shapes -->
<!-- ... -->
</Canvas>
在此画布上,我想添加一个动态列表,其中每个条目都转换为一个多边形。我认为最好的方法是ItemsControl。这是我在我的方法中使用的,但只显示集合(列表)中的第一项。
<Canvas>
<!-- ... -->
<!-- Same canvas as earlier with the addition of the ItemsControl -->
<ItemsControl ItemsSource="{Binding Offices, Mode=OneWay, Converter={...}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Polygon Fill="AliceBlue" Stroke="Gray" StrokeThickness="1" Points="{Binding Points}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
使用此代码,仅显示Offices 集合中的第一项。怎么会?如果我查看可视化树,所有多边形都在其中。我对 WPF 很陌生,所以我只能猜测,但我的第一个想法是 StackPanel 的默认设置为 ItemPresenter 在这种情况下可能不合适,但我只能猜测......
【问题讨论】:
-
我不这么认为,因为如果我反转集合(所以首先是不同的
Polygon),它甚至不会与之前显示的Polygon重叠。 -
你在哪里设置属性“办公室”?
-
在我的视图模型构造函数中,与此画布相关(
Canvas位于UserControl中,与我的视图模型松散耦合)。 -
您的
Floor集合中有哪些类型的对象? -
我不相信我提到了
Floor集合,但Storage区域被描述为Polygon。所有项目都应该在Storage中呈现。如果我使StoragePolygon不可见,仍然只有 1 个Office(集合中的第一个)可见(因此Storage没有更高的 z 索引)。
标签: c# wpf xaml canvas itemscontrol