views:

115

answers:

2

I have a WPF ItemsControl who's ItemsSource is bound to an observable collection of view models in MVVM. The ItemTemplate is set to the user control that I want. However, there are instances when I would like another control instead of the one specified in XAML.

How can I easily do this?

+1  A: 

If I understand you have a collection that contains two different type of object and you want 2 different template. You could build a datatemplate for each object type and leave WPF to render the right template based on the object type.

ema
+7  A: 

Use DataTemplates to map view models to views:

<ItemsControl ItemsSource="{Binding SomeCollectionOfViewModels}">
    <ItemsControl.Resources>
        <DataTemplate DataType="{x:Type local:FirstViewModel}">
            <Label>Foo</Label>
        </DataTemplate>

        <DataTemplate DataType="{x:Type local:SecondViewModel}">
            <Label>Bar</Label>
        </DataTemplate>
    </ItemsControl.Resources>
</ItemsControl>

HTH, Kent

Kent Boogaart