Table of Contents
Checkboxes

EO.Wpf TreeView offers built-in checkboxes support. This section covers the following topics:

Enabling Checkboxes

You can display a checkbox on a single TreeViewItem by setting that TreeViewItem's ShowCheckBox to true:

XAML
<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:eo="http://schemas.essentialobjects.com/wpf/"
        Title="MainWindow" Height="250" Width="350">
    <StackPanel>
        <eo:TreeView x:Name="TreeView1" Width="200" Height="200" HorizontalAlignment="Left">
            <eo:TreeViewItem Header="Parent Item 1">
                <eo:TreeViewItem Header="Child Item 1" ShowCheckBox="True"></eo:TreeViewItem>
                <eo:TreeViewItem Header="Child Item 2"></eo:TreeViewItem>
            </eo:TreeViewItem>
            <eo:TreeViewItem Header="Parent Item 2"></eo:TreeViewItem>
        </eo:TreeView>
    </StackPanel>
</Window>

The above code produces the following result:

Property ShowCheckBox is inheritable, so if it is set it on a parent node, all children node will display a checkbox:

XAML
<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:eo="http://schemas.essentialobjects.com/wpf/"
        Title="MainWindow" Height="250" Width="350">
    <StackPanel>
        <eo:TreeView x:Name="TreeView1" Width="200" Height="200" HorizontalAlignment="Left">
            <eo:TreeViewItem Header="Parent Item 1" ShowCheckBox="True">
                <eo:TreeViewItem Header="Child Item 1"></eo:TreeViewItem>
                <eo:TreeViewItem Header="Child Item 2"></eo:TreeViewItem>
            </eo:TreeViewItem>
            <eo:TreeViewItem Header="Parent Item 2"></eo:TreeViewItem>
        </eo:TreeView>
    </StackPanel>
</Window>

The above code produces the following result:

It can also be set on the TreeView:

XAML
<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:eo="http://schemas.essentialobjects.com/wpf/"
        Title="MainWindow" Height="250" Width="350">
    <StackPanel>
        <eo:TreeView x:Name="TreeView1" Width="200" Height="200" HorizontalAlignment="Left" ShowCheckBox="True">
            <eo:TreeViewItem Header="Parent Item 1">
                <eo:TreeViewItem Header="Child Item 1"></eo:TreeViewItem>
                <eo:TreeViewItem Header="Child Item 2"></eo:TreeViewItem>
            </eo:TreeViewItem>
            <eo:TreeViewItem Header="Parent Item 2"></eo:TreeViewItem>
        </eo:TreeView>
    </StackPanel>
</Window>

The above code produces the following result:

Once checkbox is enabled, you can use CheckState to get or set an item's check state.

Auto Check/Uncheck Parent/Child Item

EO.Wpf TreeView can automatically check/uncheck the parent item or all child items when a item is checked/unchecked. To automatically check a parent item, set the TreeView's AutoCheckParent property to true:

XAML
<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:eo="http://schemas.essentialobjects.com/wpf/"
        Title="MainWindow" Height="250" Width="350">
    <StackPanel>
        <eo:TreeView x:Name="TreeView1" Width="200" Height="200" 
            HorizontalAlignment="Left" ShowCheckBox="True" AutoCheckParent="True">
            <eo:TreeViewItem Header="Parent Item 1">
                <eo:TreeViewItem Header="Child Item 1"></eo:TreeViewItem>
                <eo:TreeViewItem Header="Child Item 2"></eo:TreeViewItem>
            </eo:TreeViewItem>
            <eo:TreeViewItem Header="Parent Item 2"></eo:TreeViewItem>
        </eo:TreeView>
    </StackPanel>
</Window>

When AutoCheckParent is set to true, the parent item's checkbox status will be automatically updated when a child item is checked or unchecked. The following image demonstrated that when "Child Item 1" is checked, "Parent Item 1" is in indeterminate state (it will be in "checked" state if all of its children items are checked).

You can also set AutoCheckChildren to true. When AutoCheckChildren is set to true, checking/unchecking a parent item automatically check/uncheck all its children items.

Handling CheckStateChanged event

TreeViewItem exposes CheckStateChanged event which is fired when the TreeViewItem's CheckState is changed. Because CheckStateChanged is a routed event, you can handle it on the TreeView:

<eo:TreeView Width="200" Height="200" HorizontalAlignment="Left" 
    ShowCheckBox="True" eo:TreeViewItem.CheckStateChanged="TreeView_CheckStateChanged">
    <eo:TreeViewItem Header="Parent Item 1">
        <eo:TreeViewItem Header="Child Item 1"></eo:TreeViewItem>
        <eo:TreeViewItem Header="Child Item 2"></eo:TreeViewItem>
    </eo:TreeViewItem>
    <eo:TreeViewItem Header="Parent Item 2">
        <eo:TreeViewItem Header="Child Item 3"></eo:TreeViewItem>
        <eo:TreeViewItem Header="Child Item 4"></eo:TreeViewItem>
        <eo:TreeViewItem Header="Child Item 5"></eo:TreeViewItem>
    </eo:TreeViewItem>
</eo:TreeView>

In the above code, even though the event is raised on the TreeViewItem objects, it is handled on the TreeView. Inside the handler you can cast the event argument's Source property to a TreeViewItem object.