Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF将不同形状绑定到ItemsControl中的DataTemplate_C#_Wpf_Data Binding_Datatemplate_Drawing2d - Fatal编程技术网

C# WPF将不同形状绑定到ItemsControl中的DataTemplate

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&

我想将不同的System.Windows.Shapes.Shape绑定到ItemsControl中的数据模板。 我有以下项目控制基于位置和形状信息的数组在画布上绘制形状:

<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; }
}