Hi
I am using a PanelBar with HierarcicalDataTemplate, and would like to define different ItemTemplates, and use them based on some data bound to the control.
XAML:
<Border Margin="0">
<Border.Resources>
<DataTemplate x:Key="CheckItem">
<StackPanel Orientation="Horizontal" Margin="10 0 0 0">
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Margin="0 0 5 0" Click="Checkbox_Click" />
<TextBlock Name="txtCriteria" Text="{Binding Description}" HorizontalAlignment="Left" MouseDown="TextBlock_MouseDown" Cursor="{Binding LinkCursor}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="TextItem">
<StackPanel Orientation="Horizontal" Margin="10 0 0 0">
<TextBlock Text="{Binding Description}" HorizontalAlignment="Left" />
<TextBox Width="50" HorizontalAlignment="Right" />
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="Category" ItemsSource="{Binding Items}" ItemTemplate="{StaticResource CheckItem}">
<StackPanel Orientation="Horizontal" Margin="0">
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" IsThreeState="True" Margin="0 4 5 6" Click="CategoryCheckbox_Click" />
<TextBlock Text="{Binding Description}" Height="13" Margin="5 4 5 6" HorizontalAlignment="Left"/>
</StackPanel>
</HierarchicalDataTemplate>
</Border.Resources>
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10">
<telerik:RadComboBox Name="rcbDefinition" Margin="0,0,0,0" Height="20"
HorizontalAlignment="Stretch" VerticalAlignment="Top"
SelectionChanged="rcbDefinition_SelectionChanged" FontSize="12" />
<telerik:RadPanelBar BorderThickness="1" BorderBrush="#FF6699CC" Name="rpbSearchCategories" Margin="0,80,0,0" Width="250" HorizontalAlignment="Left"
ExpandMode="Single" ItemTemplate="{StaticResource Category}" Expanded="rpbSearchCategories_Expanded" Background="White" />
</Grid>
</Border>
codebehind:
...
rcbDefinition.ItemsSource = SearchConfig.Items;
...
private void rcbDefinition_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SearchDefinition srchDef = (SearchDefinition)rcbDefinition.SelectedItem;
if (srchDef.Items == null)
srchDef.LoadItems();
rpbSearchCategories.ItemsSource = srchDef.DisplayItems;
txtSubHeader.Text = srchDef.Instructions;
UpdateQuery();
}
I defined two DataTemplates, one called CheckItem and one TextItem.
What I would like to accomplish is to define which one to use, based on a field on the SearchDefinition.DisplayItem class.
Taking a wild shot, I would write something like this:
<HierarchicalDataTemplate x:Key="Category" ItemsSource="{Binding Items}" ItemTemplate="{Binding CategoryType}">
But realizing this was too wild a shot.
The "CategoryType" field on the Categories in the list Items would then return either "CheckItem" or "TextItem", depending on whether the underlying items should have a checkbox or a textbox for input by the user.
I hope my question i clear enough for someone to give me a hint on how to do this!
Regards
Jonas