Click here to Skip to main content
15,434,147 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

since a while im using the TreeViews with Checkboxes in WPF.
I found a good solution here on this page:

However, I have problems to bring in a TemplateSelector correct into this code.
I just want mark lines with different colors and for this I want use a template selector. In my solution it use a template from the template selector but it does not show all elements anymore, just the first 3 subelements:

What I have tried:

  Title="TreeView with CheckBoxes"
  Width="671.601" Height="343.504" 
            <Style x:Key="TreeViewItemStyle" TargetType="TreeViewItem">
                <Setter Property="IsExpanded" Value="True" />
                <Setter Property="IsSelected" Value="{Binding IsInitiallySelected, Mode=OneTime}" />
                <Setter Property="KeyboardNavigation.AcceptsReturn" Value="True" />
            <DataTemplate x:Key="labelTemplate1">
                <StackPanel Orientation="Horizontal" >
                    <CheckBox Focusable="False" IsChecked="{Binding IsChecked}" VerticalAlignment="Center"/>
                    <TextBlock Text="{Binding Path=Name, Mode=OneWay}" Foreground="Red" Width="Auto" TextWrapping="Wrap"></TextBlock>
            <DataTemplate x:Key="labelTemplate2">
                <StackPanel Orientation="Horizontal">
                    <!-- These elements are bound to a FooViewModel object. -->
                    <Image Source="doc.png" Margin="10,0,0,0" Width="12"/>
                    <CheckBox Focusable="False" IsChecked="{Binding IsChecked}" VerticalAlignment="Center"/>
                    <TextBlock Text="{Binding Path=Name, Mode=OneWay}" Foreground="Blue" Width="Auto" TextWrapping="Wrap"></TextBlock>
            <local:LabelTemplateSelector x:Key="labelTemplateSelector" LabelTemplate1="{StaticResource labelTemplate1}" LabelTemplate2 ="{StaticResource labelTemplate2}"/>
            <HierarchicalDataTemplate x:Key="CheckBoxItemTemplate" ItemsSource="{Binding Children, Mode=OneTime}" ItemTemplateSelector="{StaticResource labelTemplateSelector}">
        <ObjectDataProvider MethodName="CreateFoos" ObjectType="{x:Type local:FooViewModel}" />
    <Grid Margin="0,0,0,-0.906">
        <Grid Height="233.746" VerticalAlignment="Top">
            <TreeView x:Name="tree" ItemContainerStyle="{StaticResource TreeViewItemStyle}" ItemsSource="{Binding Mode=Default}" ItemTemplate="{StaticResource CheckBoxItemTemplate}"/>
Dirk Bahle 27-Jun-18 15:59pm     CRLF
This question cannot be answered unless you show at least the code for the template seletor. Have you tried to set a break point in the template selection method of the LabelTemplateSelector? What happens there? Could it be that the template selector returns null under some conditions?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900