/// <summary> /// 设置元素状态 /// </summary> /// <param name="value">选中状态</param> /// <param name="updateChildren">是否更新子元素</param> /// <param name="updateParent">是否更新父元素</param> public void SetIsChecked(bool?value, bool updateChildren, bool updateParent) { if (value == mIsChecked) { return; } mIsChecked = value; if (updateChildren && mIsChecked.HasValue) { foreach (var child in Children) { ICheckableTreeItem checkableItem = child as ICheckableTreeItem; if (checkableItem != null) { checkableItem.SetIsChecked(mIsChecked, true, false); } } } if (updateParent && mParent != null) { ICheckableTreeItem parent = mParent as ICheckableTreeItem; if (parent != null) { parent.VerifyCheckState(); } } SubPropertyChanged("IsChecked"); }
/// <summary> /// 检查元素状态 /// </summary> public void VerifyCheckState() { bool?state = null; for (int i = 0; i < Children.Count; ++i) { ICheckableTreeItem checkableItem = Children[i] as ICheckableTreeItem; if (checkableItem == null) { continue; } bool?current = checkableItem.IsChecked; if (i == 0) { state = current; } else if (state != current) { state = null; break; } } SetIsChecked(state, false, true); }