且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据类型选择数据模板

更新时间: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>