WPF:树状图分组三种状态,TreeView与CheckBox

TreeView与CheckBox父节点和子节点

  • 效果图
    • 需求说明
  • 代码
    • .xaml前台
    • .xaml.cs后台
      • CheckTreeView.cs类

效果图

WPF:树状图分组三种状态,TreeView与CheckBox_第1张图片

需求说明

这是一个树状图,要求三种状态:全部选中,对号勾选;全部未选中,勾选框空白;部分选中,部分未选中,勾选框涂黑。
这是加载状态就该显示的,同时,你点击父节点选中,子节点该分组便所有都被选中;父节点为被选中,子节点就为空白。
同理,子节点也是,全部选中子节点,父节点就被勾选;都不选中子节点,父节点就空白;部分选中子节点,父节点就为填黑状态。

数据是从Mysql数据库的存储过程里动态生成的,不是写死的。

代码

.xaml前台

<Grid>
<TreeView Name="checkView" Grid.Column="0" Margin="0,50,0,0">
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded}"></Setter>
            </Style>
        </TreeView.ItemContainerStyle>
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type localCon:CheckTreeView}" ItemsSource="{Binding ChildrenView}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding Path=ViewChecked}" 
                     Tag="{Binding Path=ID}" IsTabStop="False" Focusable="False" 
                     HorizontalAlignment="Center" ClickMode="Press" Click="CheckBox_Click"></CheckBox>
                    <TextBlock Foreground="White" FontSize="12" Text="{Binding Path=ViewName}" HorizontalAlignment="Center"/>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
 </TreeView>
 </Grid>

注释:
1.TreeViewItem:wpf原生的,表示树形列表项目
2.CheckTreeView:表示该树状图数据类型来源于这个类
3.ChildrenView:CheckTreeView该类里子节点集合
4.ViewChecked:每一个节点的状态,就是那个勾选框状态
5.ViewName:勾选框后面的名字

.xaml.cs后台

//点击方法
 private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox ck = (CheckBox)sender;
            try
            {
               int fid = Convert.ToInt32(ck.Tag);
                //新建存储过程 修改物标显示DISPLAY字段 fid 为对应物标id
            }
            catch(FormatException es)
            {
                //说明是群组id
                string ids = ck.Tag.ToString().Trim('.');
                //新建存储过程 修改物标显示DISPLAY字段 ids 为对应群组 id
            }

        }

CheckTreeView.cs类

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CableMonitor.UserControls.LayoutDocuments
{

    //自定义的treeView类
    public class CheckTreeView : INotifyPropertyChanged
    {
        public Boolean? viewChecked;


        public string ViewName { get; set; }

        public string ID { get; set; }
        public List<CheckTreeView> ChildrenView { get; set; }
        public List<CheckTreeView> AllView = new List<CheckTreeView>();

        public CheckTreeView Parent;
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        //获取框选或改变框选时调用
        public Boolean? ViewChecked
        {
            get
            {
                return this.viewChecked;
            }
            set
            {
                if(viewChecked != value)
                {
                    this.viewChecked = value;
                    NotifyPropertyChanged("ViewChecked");
                    if (ChildrenView != null && ChildrenView.Count > 0)
                    {
                        if (this.viewChecked == true)
                        {
                            foreach (CheckTreeView item in ChildrenView)
                            {
                                item.ViewChecked = true;

                            }
                        }
                        if (this.viewChecked == false)
                        {
                            foreach (CheckTreeView item in ChildrenView)
                            {
                                item.ViewChecked = false;
                            }

                        }

                    }
                    else
                    {
                        if (this.Parent != null)
                        {
                            int trueCount = 0;
                            int falseCount = 0;
                            foreach (CheckTreeView item in this.Parent.ChildrenView)
                            {
                                if (item.viewChecked == true)
                                {
                                    trueCount++;
                                }

                                if (item.viewChecked == false)
                                {
                                    falseCount++;
                                }
                            }
                            if (trueCount == 0 && falseCount == 0)
                            {
                                this.Parent.ViewChecked = false;
                            }
                            if (trueCount > 0 && falseCount > 0)
                            {
                                this.Parent.ViewChecked = null;
                            }
                            if (trueCount > 0 && falseCount == 0)
                            {
                                this.Parent.ViewChecked = true;
                            }
                            if (trueCount == 0 && falseCount > 0)
                            {
                                this.Parent.ViewChecked = false;
                            }

                        }

                    }
                }
            }
        }

        //初始化数据源
        public List<CheckTreeView> GetAllViewItemFromDataTable(DataTable dataGroup, DataTable dataInfo)
        {

            for (int i = 0; i < dataGroup.Rows.Count + 1; i++)
            {
                CheckTreeView ck = new CheckTreeView();
                if (i < dataGroup.Rows.Count)
                {
                    ck.ViewName = dataGroup.Rows[i]["FgroupName"].ToString();//所有群组名称默认为FgroupName 报错请改数据库字段

                    ck.ID = dataGroup.Rows[i]["ID"].ToString() + ".";
                    DataRow[] onGroup = dataInfo.Select(" GroupId like '" + dataGroup.Rows[i]["fid"].ToString() + "'");//所有群组内物标名称默认为GroupId 报错请改数据库字段

                    Boolean? isCheck = false;
                    for (int j = 0; j < onGroup.Length; j++)
                    {
                        CheckTreeView ckChil = new CheckTreeView();
                        ckChil.viewChecked = Convert.ToInt32(onGroup[j]["IsCheck"].ToString()) == 1 ? true : false;
                        ckChil.Parent = ck;
                        if (ckChil.viewChecked == true)
                        {
                            isCheck = true;

                        }
                        else
                        {
                            if (isCheck == true)
                            {
                                isCheck = null;
                            }
                        }
                        ckChil.ViewName = onGroup[j]["Fobjname"].ToString();
                        ckChil.ID = onGroup[j]["ID"].ToString();
                        if (ck.ChildrenView == null)
                        {
                            ck.ChildrenView = new List<CheckTreeView>();
                        }
                        ck.ChildrenView.Add(ckChil);
                    }
                    ck.ViewChecked = isCheck;

                }
                else
                {
                    ck.ViewName = "未分组标绘";

                    ck.ID = null;
                    DataRow[] onGroup = dataInfo.Select(" GroupId = ''");//所有群组内物标名称默认为GroupId 报错请改数据库字段

                    Boolean? isCheck = false;
                    for (int j = 0; j < onGroup.Length; j++)
                    {
                        CheckTreeView ckChil = new CheckTreeView();
                        ckChil.viewChecked = Convert.ToInt32(onGroup[j]["IsCheck"].ToString()) == 1 ? true : false;
                        ckChil.Parent = ck;
                        if (ckChil.viewChecked == true)
                        {
                            isCheck = true;

                        }
                        else
                        {
                            if (isCheck == true)
                            {
                                isCheck = null;
                            }
                        }
                        ckChil.ViewName = onGroup[j]["Fobjname"].ToString();
                        ckChil.ID = onGroup[j]["ID"].ToString();
                        if (ck.ChildrenView == null)
                        {
                            ck.ChildrenView = new List<CheckTreeView>();
                        }
                        ck.ChildrenView.Add(ckChil);
                    }
                    ck.ViewChecked = isCheck;
                }
                AllView.Add(ck);

            }
            return AllView;
        }


    }
}

你可能感兴趣的:(WPF,wpf,c#)