Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional DataTemplate

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.

like image 913
user1175793 Avatar asked Jan 28 '12 22:01

user1175793


People also ask

What is DataTemplate?

A data template can contain elements that are each bound to a data property along with additional markup that describes layout, color and other appearance. DataTemplate is, basically, used to specify the appearance of data displayed by a control not the appearance of the control itself.

Where do you put DataTemplate?

According to Microsofts App Studio the DataTemplates should live in a DataTemplates Subdirectory under the Views Directory.

What is WPF item template?

You use the ItemTemplate to specify the visualization of the data objects. If your ItemsControl is bound to a collection object and you do not provide specific display instructions using a DataTemplate, the resulting UI of each item is a string representation of each object in the underlying collection.


1 Answers

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> 
like image 162
H.B. Avatar answered Sep 22 '22 14:09

H.B.