更新时间:2023-11-30 22:24:10
您的问题可能是由XAML的精细工作引起的.具体来说,您需要将Type
传递给DataType
,但是您传递的是带有类型名称的字符串.
Your issue might be caused by finnicky workings of XAML. Specifically, you need to pass Type
to DataType
, but you were passing a string with the name of the type.
使用x:Type
装饰DataType
的值,如下所示:
Use x:Type
to decorate the value of DataType
, like so:
<ItemsControl ItemsSource="{Binding Coll}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:ClassOne}">
<Rectangle Width="50" Height="50" Fill="Red" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:ClassTwo}">
<Rectangle Width="50" Height="50" Fill="Blue" />
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>