WPF TreeView 带复选框

//前台界面

 <TreeView Name="TreeView_NodeList" BorderThickness="1" Margin="10,10,10,35">
            <TreeView.Resources>
                <HierarchicalDataTemplate  DataType="{x:Type local:Node}" ItemsSource="{Binding Nodes,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                    <StackPanel Orientation="Horizontal" Margin="0,2,0,2">
                        <CheckBox Content="{Binding NodeName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ToolTip="{Binding NodeName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsChecked="{Binding IsSelected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" ></CheckBox>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="True" />
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>

//后台代码

        public List<Node> InitialNodeList { get; set; }//初始化集合数据

        InitializeNodes();//初始化节点
        this.TreeView_NodeList.ItemsSource = InitialNodeList;//显示节点

        /// <summary>
        /// 初始化节点
        /// </summary>
        /// <returns></returns>
        private void InitializeNodes()
        {
            Node leafOneNode = new Node();
            leafOneNode.NodeID = 10000;
            leafOneNode.NodeName = "DKL-200x500";

            Node leafTwoNode = new Node();
            leafTwoNode.NodeID = 10001;
            leafTwoNode.NodeName = "DKL-200x600";

            Node leafThreeNode = new Node();
            leafThreeNode.NodeID = 10002;
            leafThreeNode.NodeName = "DKL-250x600";

            var nodes = new List<Node>();
            nodes.Add(leafOneNode);
            nodes.Add(leafTwoNode);
            nodes.Add(leafThreeNode);
            var node1 = new Node();
            node1.NodeName = CurrentCategoryName;
            node1.Nodes = nodes;

            InitialNodeList = new List<Node>();
            InitialNodeList.Add(node1);
        }

        /// <summary>
        /// 勾选节点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if ((sender as CheckBox).Content.ToString() == "全选")
            {
                foreach (Node item in InitialNodeList[0].Nodes)
                {
                    item.IsSelected = true;
                }
            }
        }

        /// <summary>
        /// 去除勾选节点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if ((sender as CheckBox).Content.ToString() == “全选”)
            {
                foreach (Node item in InitialNodeList[0].Nodes)
                {
                    item.IsSelected = false;
                }
            }
        }

 

 public class Node : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        public Node()
        {
            this.IsSelected = false;
            this.Nodes = new List<Node>();
        }

        public int NodeID { get; set; }

        /// <summary>
        ///  节点名称
        ///</summary>
        public string NodeName { get; set; }

        private bool? isSelected = false;
        /// <summary>
        /// 是否勾选
        /// </summary>
        public bool? IsSelected
        {
            set
            {
                this.isSelected = value;
                this.NotifyPropertyChanged("IsSelected");
            }
            get
            {
                return this.isSelected;
            }
        }

        /// <summary>
        ///  子节点集合
        /// </summary>
        public List<Node> Nodes { get; set; }
    }

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页