简体   繁体   中英

In C#, .Net 3.5, how can I add checkboxes to a TreeView?

This is a codebase that I inherited, and the TreeView in the System.Windows.Controls namespace apparently does not support the CheckBoxes property. I have tried to switch to System.Windows.Forms, but too much code had to be changed. Is it possible to add checkboxes to a System.Windows.Control.TreeView, and if so, how? Thank you.

You can by affecting a HierarchicalDataTemplate to the treeview's ItemTemplate property. Just define your template to something like this:

<HierarchicalDataTemplate x:Key="CheckBoxItemTemplate" ItemsSource="{Binding YourNodeInnerElementsCollectionHere, Mode=OneTime}">
  <StackPanel Orientation="Horizontal">
    <CheckBox Focusable="False" IsChecked="{Binding YourBooleanPropertyForCheck}" VerticalAlignment="Center"/>
    <ContentPresenter Content="{Binding YourStringPropertyForText, Mode=OneTime}"/>
  </StackPanel>
</HierarchicalDataTemplate>

And then set your treeview as follow:

<TreeView (...) ItemTemplate="{StaticResource CheckBoxItemTemplate}" (...) />

UPDATE

Here's a very trivial example of a templated treeview.

Project : WpfApplication4 (root namespace)

File 1: MyItemCollection.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApplication4
{
    public class MyItemCollection : System.Collections.ObjectModel.ObservableCollection<MyItem>
    {

        public MyItemCollection()
        {
            Add(new MyItem() { MyText = "test", MyIsChecked = true });
            Add(new MyItem() { MyText = "test2", MyIsChecked = false  });
            Add(new MyItem() { MyText = "test3", MyIsChecked = false });
            Add(new MyItem() { MyText = "test4", MyIsChecked = true });

            this[0].MyInnerCollection.Add(new MyItem() { MyText = "innertest", MyIsChecked = true });
        }

    }

    public class MyItem
    {

        public MyItem()
        {
            _MyInnerCollection = new System.Collections.ObjectModel.ObservableCollection<MyItem>();
        }

        // Fields...
        private System.Collections.ObjectModel.ObservableCollection<MyItem> _MyInnerCollection;
        private bool _MyIsChecked;
        private string _MyText;

        public string MyText
        {
            get { return _MyText; }
            set
            {
                _MyText = value;
            }
        }

        public bool MyIsChecked
        {
            get { return _MyIsChecked; }
            set
            {
                _MyIsChecked = value;
            }
        }

        public System.Collections.ObjectModel.ObservableCollection<MyItem> MyInnerCollection
        {
            get
            {
                return _MyInnerCollection;
            }
        }

    }
}

File 2 : MainWindow.xaml

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication4"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <local:MyItemCollection x:Key="ColObj"></local:MyItemCollection>
            <HierarchicalDataTemplate x:Key="CheckBoxItemTemplate" ItemsSource="{Binding MyInnerCollection}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox Focusable="False" IsChecked="{Binding MyIsChecked}" VerticalAlignment="Center"/>
                    <ContentPresenter Content="{Binding MyText}"/>
                </StackPanel>
            </HierarchicalDataTemplate>
        </Grid.Resources>
        <TreeView ItemTemplate="{StaticResource CheckBoxItemTemplate}" ItemsSource="{StaticResource ColObj}">
        </TreeView>
    </Grid>
</Window>

Again, that's for demonstration purpose. :) It should give you the main idea around templates for treeview items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM