wpf 带复选框的treeview_WPF:带复选框CheckBox的树TreeView
public class CommonTreeView : NotifyPropertyBase
{
// 父
///public CommonTreeView Parent
{
get;
set;
}
// 子
///public ListChildren
{
get;
set;
}
// 節點的名字
///public string NodeName
{
get;
set;
}
public bool? _isChecked;
// CheckBox是否選中
///public bool? IsChecked
{
get
{
return _isChecked;
}
set
{
SetIsChecked(value, true, true);
}
}
public CommonTreeView(string name)
{
this.NodeName=name;
this.Children=new List();
}
public CommonTreeView() { }
private void SetIsChecked(bool? value, bool checkedChildren, bool checkedParent)
{
if (_isChecked == value) return;
_isChecked = value;
//選中和取消子類
if (checkedChildren && value.HasValue && Children != null)
Children.ForEach(ch => ch.SetIsChecked(value, true, false));
//選中和取消父類
if (checkedParent && this.Parent != null)
this.Parent.CheckParentCheckState();
//通知更改
this.SetProperty(x => x.IsChecked);
}
// 檢查父類是否選 中
/// 如果父類的子類中有一個和第一個子類的狀態不一樣父類ischecked為null
///private void CheckParentCheckState()
{
List checkedItems = new List();
string checkedNames = string.Empty;
bool? _currentState = this.IsChecked;
bool? _firstState = null;
for (int i = 0; i
{
bool? childrenState= this.Children[i].IsChecked;if (i==0)
{
_firstState= childrenState;}
else if (_firstState != childrenState){
_firstState= null;}
}
if (_firstState != null)_currentState= _firstState;SetIsChecked(_firstState, false, true);
}
/// /// 創建樹
/public void CreateTreeWithChildre( CommonTreeView children,bool? isChecked)
{
this.Children.Add(children);
//必須先把孩子加入再為Parent賦值,
//否則當只有一個子節點時Parent的IsChecked狀態會出錯
children.Parent = this;
children.IsChecked = isChecked;
}
}
總結
以上是生活随笔為你收集整理的wpf 带复选框的treeview_WPF:带复选框CheckBox的树TreeView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: foxmail邮箱怎么导入邮件_163企
- 下一篇: java web.xml_Java We