Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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# ItemsControl忽略资源中的数据模板_C#_Wpf_Xaml_Datatemplate_Itemscontrol - Fatal编程技术网

C# ItemsControl忽略资源中的数据模板

C# ItemsControl忽略资源中的数据模板,c#,wpf,xaml,datatemplate,itemscontrol,C#,Wpf,Xaml,Datatemplate,Itemscontrol,我想将多个类型的集合绑定到画布中显示的ItemsControl。到目前为止,我的代码如下: <ItemsControl ItemsSource="{Binding Path=Objects}"> <ItemsControl.Resources> <DataTemplate DataType="self:CalibrationRectangle"> <Rectangle Fill="{Binding P

我想将多个类型的集合绑定到画布中显示的ItemsControl。到目前为止,我的代码如下:

<ItemsControl ItemsSource="{Binding Path=Objects}">
     <ItemsControl.Resources>
         <DataTemplate DataType="self:CalibrationRectangle">
             <Rectangle Fill="{Binding Path=Color, Converter={StaticResource ColorToBrushConverter}}" Width="{Binding Width}" Height="{Binding Height}" />
         </DataTemplate>
         <DataTemplate DataType="self:PolygonVM">
             <Polygon Fill="{Binding Path=Color, Converter={StaticResource ColorToBrushConverter}}" Points="{Binding Points}" />
         </DataTemplate>
     </ItemsControl.Resources>
     <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
             <Canvas Background="Red" />
         </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
     <!--<ItemsControl.ItemTemplate>
         <DataTemplate DataType="self:CalibrationRectangle">
             <Rectangle Fill="{Binding Path=Color, Converter={StaticResource ColorToBrushConverter}}" Width="{Binding Width}" Height="{Binding Height}" />
         </DataTemplate>
     </ItemsControl.ItemTemplate>-->
     <ItemsControl.ItemContainerStyle>
         <Style>
             <Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
             <Setter Property="Canvas.Left" Value="{Binding Path=X}" />
         </Style>
     </ItemsControl.ItemContainerStyle>
</ItemsControl>
在这种代码状态下,资源中的数据模板被完全忽略,而对象被表示为字符串。 相反,如果我使用注释掉的行并在ItemsControl.ItemsTemplate中定义DataTemplate,它就会工作。但是,由于我需要多个数据模板,每种类型一个,所以这不是解决方案

这里有什么问题?为什么资源中的数据模板不起作用


非常感谢您的任何想法:

在您以XML元素名称为目标的时候。如果要将类型作为目标,则需要使用{x:type…}并替换

<DataTemplate DataType="self:CalibrationRectangle">

发件人:

要引用类的类型名称,请使用x:type标记扩展名。如果模板用于XML数据,则此属性包含XML元素名称


此时,您的目标是XML元素名。如果要将类型作为目标,则需要使用{x:type…}并替换

<DataTemplate DataType="self:CalibrationRectangle">

发件人:

要引用类的类型名称,请使用x:type标记扩展名。如果模板用于XML数据,则此属性包含XML元素名称


什么是自校准矩形和自校准多边形虚拟机?因为在我看来,您正在将DataTemplate应用于ItemsControl中未使用的类型,这些类型只是一些自己的类型,具有绑定字段作为属性。它们都有相同的父类型DrawableVM。绑定的集合对象就是一个可观察的集合。所以我的ItemsControl应该使用这些类型,还是我得到了一些东西?什么是self:CalibrationRectangle和selfPolygonVM?因为在我看来,您正在将DataTemplate应用于ItemsControl中未使用的类型,这些类型只是一些自己的类型,具有绑定字段作为属性。它们都有相同的父类型DrawableVM。绑定的集合对象就是一个可观察的集合。所以我的ItemsControl应该使用这些类型,或者我正在工作?谢谢:它现在正在工作。但是你能解释一下这两者之间的区别吗?我刚刚编辑了我的答案以添加更多信息,但基本上没有添加目标XML元素名称谢谢:它现在起作用了。但是你能解释一下这两者之间的区别吗?我刚刚编辑了我的答案以添加更多信息,但基本上没有以XML元素名为目标