treeview形式的checkbox(wpf、c#)

如何实现treeview形式的checkbox,并且父节点和子节点的选中状态可相互影响。示例图:

代码如下:

wpf代码:treeview绑定的数据是PermissionDataCollection。

csharp 复制代码
<TreeView ItemsSource="{Binding PermissionDataCollection}">
	<TreeView.ItemTemplate>
		<HierarchicalDataTemplate ItemsSource="{Binding Children}">
			<StackPanel Orientation="Horizontal" >
				<CheckBox IsChecked="{Binding IsChecked}"/>
				<TextBlock Text="{Binding Name}"/>
			 </StackPanel>
		</HierarchicalDataTemplate>
	</TreeView.ItemTemplate>
</TreeView>

后端数据来源:PermissionDataCollection的声明及定义:

csharp 复制代码
public ObservableCollection<TreeViewItemModel> PermissionDataCollection { get; set; }
csharp 复制代码
PermissionDataCollection = new ObservableCollection<TreeViewItemModel>();

// 添加根节点
var rootNode = new TreeViewItemModel { Name = "所有权限", IsChecked = false, Children = new ObservableCollection<TreeViewItemModel>() };
PermissionDataCollection0.Add(rootNode);

// 添加子节点
var childNode1 = new TreeViewItemModel { Name = "系统设置", IsChecked = false, Children = new ObservableCollection<TreeViewItemModel>(), Parent = rootNode };
var childNode2 = new TreeViewItemModel { Name = "日志管理", IsChecked = true, Children = new ObservableCollection<TreeViewItemModel>(), Parent = rootNode };
rootNode.Children.Add(childNode1);
rootNode.Children.Add(childNode2);

// 添加孙子节点
var grandChildNode1 = new TreeViewItemModel { Name = "相机设置", IsChecked = true, Children = new ObservableCollection<TreeViewItemModel>(), Parent = childNode1 };
var grandChildNode2 = new TreeViewItemModel { Name = "参数设置", IsChecked = false, Children = new ObservableCollection<TreeViewItemModel>(), Parent = childNode1};
childNode1.Children.Add(grandChildNode1);
childNode1.Children.Add(grandChildNode2);

TreeViewItemModel的定义:(也就是定义treeview形式的checkbox)

csharp 复制代码
public class TreeViewItemModel : INotifyPropertyChanged
{
  private bool _isChecked;
  private bool _isUpdatingChildren;

  public string Name { get; set; }

  public bool IsChecked
  {
      get { return _isChecked; }
      set
      {
          if (_isChecked != value)
          {
              _isChecked = value;
              OnPropertyChanged(nameof(IsChecked));

              if (!_isUpdatingChildren && Children != null)
              {
                  _isUpdatingChildren = true;

                  // 递归设置子节点的勾选状态
                  foreach (var child in Children)
                  {
                      child.IsChecked = value;
                  }

                  _isUpdatingChildren = false;
              }

              // 更新父节点的勾选状态
              if (Parent != null)
              {
                  UpdateParentCheckedState();
              }
          }
      }
  }

  public ObservableCollection<TreeViewItemModel> Children { get; set; }

  public TreeViewItemModel Parent { get; set; }

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(string propertyName)
  {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  }

  public void UpdateParentCheckedState()
  {
      if (Parent != null)
      {
          bool allSiblingsChecked = Parent.Children.All(child => child.IsChecked);
          bool anySiblingChecked = Parent.Children.Any(child => child.IsChecked);

          if (allSiblingsChecked)
          {
              Parent._isChecked = true;
          }
          else if (anySiblingChecked)
          {
              Parent._isChecked = false;
          }
          else
          {
              Parent._isChecked = false;
          }

          Parent.OnPropertyChanged(nameof(IsChecked));
          Parent.UpdateParentCheckedState();
      }
  }
}
相关推荐
喵叔哟2 分钟前
重构代码中引入外部方法和引入本地扩展的区别
java·开发语言·重构
尘浮生8 分钟前
Java项目实战II基于微信小程序的电影院买票选座系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
hopetomorrow22 分钟前
学习路之PHP--使用GROUP BY 发生错误 SELECT list is not in GROUP BY clause .......... 解决
开发语言·学习·php
小牛itbull32 分钟前
ReactPress vs VuePress vs WordPress
开发语言·javascript·reactpress
广煜永不挂科40 分钟前
Devexpress.Dashboard的调用二义性
c#·express
请叫我欧皇i41 分钟前
html本地离线引入vant和vue2(详细步骤)
开发语言·前端·javascript
闲暇部落43 分钟前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
当下就是最好1 小时前
WPF应用程序的生命周期-笔记
wpf
GIS瞧葩菜1 小时前
局部修改3dtiles子模型的位置。
开发语言·javascript·ecmascript
chnming19871 小时前
STL关联式容器之set
开发语言·c++