ItemScontrol忽略了资源中的数据模拟



我想将多种类型的集合绑定到在画布中显示的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">

<DataTemplate DataType="{x:Type self:CalibrationRectangle}">

来自msdn:

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

最新更新