资源简介

在wpf中给treeview动态添加子节点,子节点带有checkbox,和右键弹出菜单。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Globe3DWPF
{
    /// 
    /// TreeViewCheckbox.xaml 的交互逻辑
    /// 

    public partial class TreeViewCheckbox : Window
    {
        public TreeViewCheckbox()
        {
            InitializeComponent();

            AddTreeviewItem(this.treeView1);
        }

        private void AddTreeviewItem(TreeView tvitems)
        {
       
            for (int i = 0; i < 10; i++)
            {
                TreeViewItem tvm = new TreeViewItem();
                tvm.Header = i.ToString();

                tvitems.Items.Add(tvm);

                for (int j = 0; j < 5; j++)
                {
                    TreeViewItem tv = new TreeViewItem();

                    CheckBox ckb=new CheckBox();
                    ckb.Click += new RoutedEventHandler(ckb_Click);
                    ckb.Content=i.ToString()+“ “+j.ToString();

                    tv.Header = ckb;

                    tvm.Items.Add(tv);
                }
            }
        }

        void ckb_Click(object sender RoutedEventArgs e)
        {
            CheckBox ckb = sender as CheckBox;

            if (ckb != null)
            {
                if (ckb.IsChecked == true)
                {
                    this.textBox1.Text = ckb.Content.ToString()+“ 已经选择“;
                }
                else
                {
                    this.textBox1.Text = ckb.Content.ToString() + “  不选择“;
                }
            }

           
        }

        private void treeView1_SelectedItemChanged(object sender RoutedPropertyChangedEventArgsject> e)
        {
            TreeViewItem tvm = this.treeView1.SelectedItem as TreeViewItem;

            CheckBox ckb = tvm.Header as CheckBox;

            if (ckb != null)
            {
                MessageBox.Show(ckb.Content.ToString());
            }


        }

        private void OnOpened(object sender RoutedEventArgs e)
        {

        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1150  2010-04-18 23:54  wpf treeview checkbox\TreeViewCheckbox.xaml

     文件       2364  2010-04-18 23:48  wpf treeview checkbox\TreeViewCheckbox.xaml.cs

     目录          0  2010-04-19 11:05  wpf treeview checkbox

----------- ---------  ---------- -----  ----

                 3514                    3


评论

共有 条评论