I'd like to bind different System.Windows.Shapes.Shape to a DataTemplate in an ItemsControl. I have the followng ItemsControl drawing shapes on a Canvas based on an array with positions and shape informations:
<ItemsControl Width="800" ItemsSource="{Binding ShapesPositionArray}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Name="sequenceCanvas" Width="800" Height="800" ClipToBounds="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}"/>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Ellipse Width="5" Height="5" Fill="Black"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
If I bind with a Shape like Ellipse (like the example) or Rectangle or Polygon it is perfectly working, but I need to have at the same time different shapes, like Polygons and Ellipses. I tried to associate the DataTemplate to an object PartShape of type Shapes using a ContentControl:
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding PartShape}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
The PartShape on the in VM is an object like this:
public System.Windows.Shapes.Shape PartShape
{
get
{
System.Windows.Shapes.Shape r = new System.Windows.Shapes.Ellipse();
r.Width = 20;
r.Height = 5;
return r;
}
}
The binding is ok and gives no error, but it does not work, it draws nothing on canvas. How could I do?
Thanks.
you need to colorize the shape. it is added but not visible.
System.Windows.Shapes.Shape r = new System.Windows.Shapes.Ellipse
{
Width = 20,
Height = 5,
Fill = Brushes.Blue
};
public System.Windows.Shapes.Shape PartShape
{
get { return r; }
}