I need to add different controls (TextBox/CheckBox/ComboBox, etc) in ItemsControl based on certain condition. Each Item in ItemsControl is a Name-Value pair. Name is always represented by TextBlock but Value can be any UI control. I am using horizontally aligned StackPanel to represent each Item. First control in StackPanel remains TextBlock but second control is dependent upon "ItemDataType" property set in ViewModel at runtime.
The problem I have is that I am not able to assign different controls in StackPanel's 2nd element using Style trigger with ItemDataType property.
Code Snippet:
<UserControl.Resources>
<DataTemplate x:Key="TextBoxTemplate">
<TextBox Text="{Binding Path=DataValue}"/>
</DataTemplate>
<DataTemplate x:Key="ComboBoxTemplate">
<ComboBox ItemsSource="{Binding Path=SelectionList}" SelectedValue="{Binding Path=DataValue,Mode=TwoWay}"/>
</DataTemplate>
<DataTemplate x:Key="CheckBoxTemplate">
<CheckBox IsChecked="{Binding Path=DataValue,Mode=TwoWay}" />
</DataTemplate>
<DataTemplate x:Key="ButtonTemplate">
<Button Content="{Binding Path=DataValue}"/>
</DataTemplate>
<DataTemplate x:Key="dynamicTemplate">
<StackPanel Orientation="Horizontal" Tag="{Binding ItemDataType}">
<TextBlock Text="{Binding Path=DataName,Mode=TwoWay}"/>
<ContentControl>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding ItemDataType}" Value="TextBox">
<Setter Property="Template" Value="{StaticResource TextBoxTemplate}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid>
<!-- CONTROL LAYOUT -->
<ItemsControl ItemsSource="{Binding Path=DataList,Mode=TwoWay}" ItemTemplate="{StaticResource dynamicTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
Error I get is DataTemplate invalid for ContentControl.Template property. I understand that what I am doing is wrong, but I want help to do it right way.
Thanks,
RDV