C# WPF将不同形状绑定到ItemsControl中的DataTemplate
我想将不同的System.Windows.Shapes.Shape绑定到ItemsControl中的数据模板。 我有以下项目控制基于位置和形状信息的数组在画布上绘制形状:C# WPF将不同形状绑定到ItemsControl中的DataTemplate,c#,wpf,data-binding,datatemplate,drawing2d,C#,Wpf,Data Binding,Datatemplate,Drawing2d,我想将不同的System.Windows.Shapes.Shape绑定到ItemsControl中的数据模板。 我有以下项目控制基于位置和形状信息的数组在画布上绘制形状: <ItemsControl Width="800" ItemsSource="{Binding ShapesPositionArray}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate&
<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>
绑定是正常的,没有错误,但它不起作用,它在画布上什么也没画。
我怎么办
谢谢。您需要给形状上色。它已添加但不可见
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; }
}
您没有设置填充或笔划。形状在那里,但不可见
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;
}
}
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; }
}