目标效果
初始加载
选中叶子节点
操作中间节点
实现思路
TreeView
数据源集合的子项ViewModel
部分
BindableBase
为 Prism
中对应的Vm
基类,读者也可以按自身需求实现属性通知接口(INotifyPropertyChanged
)的基类。
public class ModuleNode:BindableBase
{
//public bool IsChecked { get; set; } //属性需要设置为可空类型
//复选框状态
private bool? isChecked;
public bool? IsChecked
{
get { return isChecked; }
set { SetProperty(ref isChecked, value); }
}
// 子节点集合
public List<ModuleNode> Children { get; set; }
// 父节点对象
public ModuleNode Parent { get; set; }
}
主页面ViewModel
public class MainViewModel:BindableBase
{
public DelegateCommand<ModuleNode> ModuleCkCommand { get; set; }
public MainViewModel()
{
ModuleCkCommand = new DelegateCommand<ModuleNode>(ModuleCkMethod);
}
/// <summary>
/// 模块选中函数
/// </summary>
/// <param