在C#,。Net 3.5中,如何向TreeView添加复选框?

时间:2014-01-28 17:48:32

标签: c# wpf visual-studio-2010 treeview

这是我继承的代码库,而System.Windows.Controls命名空间中的TreeView显然不支持CheckBoxes属性。我试图切换到System.Windows.Forms,但必须更改太多的代码。是否可以向System.Windows.Control.TreeView添加复选框,如果是,如何?谢谢。

1 个答案:

答案 0 :(得分:0)

您可以通过影响树视图的ItemTemplate属性HierarchicalDataTemplate。只需将模板定义为以下内容:

<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>

然后将您的树视图设置如下:

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

<强>更新

这是模板树视图的一个非常简单的例子。

项目:WpfApplication4(根名称空间)

文件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;
            }
        }

    }
}

文件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>

同样,这是出于演示目的。 :)它应该为您提供围绕树视图项目模板的主要想法。