34

Here is what I am trying to do. I have 2 Data Templates defined which both refer to a different user control.

<UserControl.Resources>
    <DataTemplate x:Key="myDataTemplate1">
        <Border BorderBrush="Black" BorderThickness="1">
            <myUserControl1 />
        </Border>
    </DataTemplate>
    <DataTemplate x:Key="myDataTemplate2">
            <Border BorderBrush="Black" BorderThickness="1">
                <myUserControl2/>
            </Border>
    </DataTemplate>
</UserControl.Resources>

I am using these Data Templates to display a list of items using ItemsControl like this:

<ItemsControl x:Name="myItemList" ItemTemplate="{StaticResource myDataTemplate1}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate />
    </ItemsControl.ItemsPanel>
</ItemsControl>

I would like the ItemTemplate to conditionally be either myDataTemplate1 or myDataTemplate1 depending on the value of an integer variable being 1 or 2 respectively.

Should I use DataTriggers for this or is there another way to do this? Appreciate the help.

Craig
  • 6,869
  • 3
  • 32
  • 52
user1175793
  • 343
  • 1
  • 3
  • 4

1 Answers1

59

Don't set the ItemTemplate but use an ItemTemplateSelector.

DataTriggers would be fine too of course, spares you the extra class for the selector. e.g.

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <ContentControl Content="{Binding}">
            <ContentControl.Style>
                <Style TargetType="ContentControl">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ThatProperty}" Value="1">
                            <Setter Property="ContentTemplate"
                                    Value="{StaticResource myDataTemplate1}" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ThatProperty}" Value="2">
                            <Setter Property="ContentTemplate"
                                    Value="{StaticResource myDataTemplate2}" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>
    </DataTemplate>
</ItemsControl.ItemTemplate>
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I did not realize it was legal to set a ContentTemplate with a DataTemplate? How does that work? – blue18hutthutt Nov 06 '12 at 23:43
  • 2
    @blue18hutthutt: `ContentTemplate` is just a property which could have any type, in this case the type is `DataTemplate` as can be seen in [the documentation](http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contenttemplate.aspx) as well. Not to be confused with `ControlTemplate` which is also a type of template which is used with the [`Template`](http://msdn.microsoft.com/en-us/library/system.windows.controls.control.template.aspx) property. Also there are analogous properties: `ContentControl`->`ContentTemplate` & `ItemsControl`->`ItemTemplate`. – H.B. Nov 07 '12 at 01:51
  • That's where the confusion stems from :) Come to think of it ..that also explains alot of other confusion from the past too! – blue18hutthutt Nov 07 '12 at 02:03