private static void SetChildCheckedState(TreeViewItemViewModel itemsControl, bool checkedstate) { itemsControl.IsChecked = checkedstate; for (int i = 0; i < itemsControl.Children.Count; i++) { TreeViewItemViewModel tvi = itemsControl.Children[i]; if (tvi != null) { tvi.IsChecked = checkedstate; SetChildCheckedState(tvi, checkedstate); } } }
//private static T FindVisualChild<T>(Visual visual) where T : Visual //{ // for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++) // { // Visual child = (Visual)VisualTreeHelper.GetChild(visual, i); // if (child != null) // { // T correctlyTyped = child as T; // if (correctlyTyped != null) // { // return correctlyTyped; // } // T descendent = FindVisualChild<T>(child); // if (descendent != null) // { // return descendent; // } // } // } // return null; //} //-------------------------------------------------------------------------- private static void SetParentCheckedState(TreeViewItemViewModel tvitem) { if (tvitem != null) { TreeViewItemViewModel parent = tvitem.Parent; if (!(parent is null) && parent.CheckBoxVisibility == Visibility.Visible) { Collection <TreeViewItemViewModel> items = parent.Children; int cntindeterminate = 0; int cntunchecked = 0; int cntchecked = 0; foreach (var i in items) { if (i.IsChecked == true) { cntchecked++; } else if (i.IsChecked == false) { cntunchecked++; } else if (i.IsChecked == null) { cntindeterminate++; } } if (cntindeterminate > 0 || (cntunchecked > 0 && cntunchecked < items.Count)) { parent.IsChecked = null; } else if (cntchecked == items.Count) { parent.IsChecked = true; } else if (cntunchecked == items.Count) { parent.IsChecked = false; } } } }
protected TreeViewItemViewModel(TreeViewItemViewModel parent, bool lazyLoadChildren) { _parent = parent; _children = new ObservableCollection <TreeViewItemViewModel>(); }