Click here to Skip to main content
15,883,883 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:
tps://www.codeproject.com/Articles/28306/Working-with-Checkboxes-in-the-WPF-TreeView


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:

<Window 
  x:Class="TreeViewWithCheckBoxes.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:TreeViewWithCheckBoxes"
  xmlns:dw="clr-namespace:DrWPF.Windows.Controls"
  FontSize="13"
  Title="TreeView with CheckBoxes"
  Width="671.601" Height="343.504" 
  WindowStartupLocation="CenterScreen"  
  >
    <Window.Resources>
        <ResourceDictionary>
            <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" />
            </Style>
            <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>
                </StackPanel>
            </DataTemplate>
            <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>
                </StackPanel>
            </DataTemplate>
            <local:LabelTemplateSelector x:Key="labelTemplateSelector" LabelTemplate1="{StaticResource labelTemplate1}" LabelTemplate2 ="{StaticResource labelTemplate2}"/>
            <HierarchicalDataTemplate x:Key="CheckBoxItemTemplate" ItemsSource="{Binding Children, Mode=OneTime}" ItemTemplateSelector="{StaticResource labelTemplateSelector}">
            </HierarchicalDataTemplate>
        </ResourceDictionary>
    </Window.Resources>
    <Window.DataContext>
        <ObjectDataProvider MethodName="CreateFoos" ObjectType="{x:Type local:FooViewModel}" />
    </Window.DataContext>
    <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}"/>
        </Grid>
    </Grid>
</Window>
Posted
Comments
Dirk Bahle 27-Jun-18 15:59pm    
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