我想在画布上添加动态数量的椭圆并设置椭圆位置(canvas.top,canvas.left)。我尝试绑定到ItemsControl,但每个项目(椭圆)都有一个容器,所以我不能直接设置椭圆位置。我不想要一个items容器,我只想要一个包含省略号的画布。
可以这样做吗?
答案 0 :(得分:2)
通常我会说ItemsControl
与Canvas
:
<ItemsControl ItemsSource="{Binding Ellipses}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemsContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Left}"/>
<Setter Property="Canvas.Top" Value="{Binding Top}"/>
</Style>
</ItemsControl.ItemsContainerStyle>
</ItemsControl>
但是在显示Silverlight suckiness时,ItemContainerStyle
属性对ItemsControl
不起作用。它在ItemsControl
中有支持,但ItemsControl
本身并未公开。相反,它取决于ItemsControl
的子类 - 例如ListBox
- 来公开它。哦,这些子类必须由Microsoft提供,因为功能是protected internal
,所以你不能只是将ItemsControl
子类化并自己公开这些东西。 :S
因此,您可以使用ListBox
,可能通过对其进行子类化并将其项容器更改为比ListBoxItem
更简单的内容。或者你可以直接使用ListBox
并摆弄,直到ListBoxItem
看起来像你想要的那样(即没有被选中)。
答案 1 :(得分:2)
试试这个 - 为我工作 - 我用它来自由地在画布上放置文本块。
Re:Re:当Canvas是ItemsControl的ItemsPanel时定位项目 02-26-2010 7:17 AM |
还有另一种更简单的解决方案,可以在silverlight 3中运行。
<Canvas>
<ItemsControl ItemsSource={Binding MyItems}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<TextBlock Canvas.Left={Binding Left} Canvas.Top={Binding Top} Text={Binding Text} />
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
如果MyItems是具有Left,Top和Text公共属性的类的项列表,则可以正常工作。我还使用Line和Border测试了在silverlight 3中绘制简单的条形图。
从这篇文章的底部: http://forums.silverlight.net/forums/p/29753/450510.aspx#450510
将它与Silverlight DataTemplateSelector结合使用,您可以根据视图模型属性更改绘制的对象:
http://www.codeproject.com/KB/silverlight/SLTemplateSelector.aspx