Selecting a data template based on type

前端 未结 1 1863
野性不改
野性不改 2020-12-16 16:56

I\'ve declared the following types:

public interface ITest { }
public class ClassOne : ITest { }
public class ClassTwo : ITest { }

In my vi

相关标签:
1条回答
  • 2020-12-16 17:13

    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.

    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>
    
    0 讨论(0)
提交回复
热议问题