Silverlight-使用MVVM向画布动态添加省略号

Silverlight-使用MVVM向画布动态添加省略号,silverlight,data-binding,mvvm,Silverlight,Data Binding,Mvvm,我想在画布上添加动态数量的椭圆,并设置椭圆位置(canvas.top,canvas.left)。我尝试绑定到ItemsControl,但每个item(椭圆)都有一个容器,因此无法直接设置椭圆的位置。我不想要项目容器,我只想要一个包含省略号的画布 可以这样做吗?通常我会说,将项控件与画布结合使用: <ItemsControl ItemsSource="{Binding Ellipses}"> <ItemsControl.ItemsPanel> <

我想在画布上添加动态数量的椭圆,并设置椭圆位置(canvas.top,canvas.left)。我尝试绑定到ItemsControl,但每个item(椭圆)都有一个容器,因此无法直接设置椭圆的位置。我不想要项目容器,我只想要一个包含省略号的画布


可以这样做吗?

通常我会说,将
项控件
画布
结合使用:

<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的显示中,
ItemContainerStyle
属性对
ItemsControl
不起作用。它在
ItemsControl
中有支持,但它不由
ItemsControl
本身公开。相反,由
ItemsControl
的子类来公开它,例如
ListBox
。哦,这些子类必须由Microsoft提供,因为其功能是受内部保护的,所以您不能只将
ItemsControl
子类化,然后自己公开这些内容

因此,您可以使用
ListBox
,也可以将其子类化,并将其项容器更改为比
ListBoxItem
更简单的内容。或者您可以直接使用
ListBox
,然后四处拨弄,直到
ListBoxItem
s的外观符合您的要求(即未选中)。

试试这个-对我有用-我使用它在画布上自由放置文本块

Re:Re:当画布是ItemsControl的ItemsPanel时定位项目 02-26-2010上午7:17

在silverlight 3中有一个更简单的替代解决方案


如果MyItems是一个具有Left、Top和Text公共属性的类的项目列表,则可以正常工作。我还测试了Line和Border在silverlight 3中绘制简单的条形图图形

从这篇文章的底部:

将其与Silverlight DataTemplateSelector结合使用,可以根据视图模型属性更改绘制的对象:


所以没有办法绕过itemContainer?我只是在我的帖子中解释了如何绕过itemContainer:S
 
 <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>