/// <summary> /// 展开所有子节点 /// </summary> public void ExpandAllChildren() { if (Items != null && Items.Count > 0) { foreach (var item in Items) { if (item is MultiSelectionTreeViewItem) { MultiSelectionTreeViewItem tvi = item as MultiSelectionTreeViewItem; if (!tvi.IsExpanded) { tvi.IsExpanded = true; } tvi.ExpandAllChildren(); } else { MultiSelectionTreeViewItem tvi = this.ItemContainerGenerator.ContainerFromItem(item) as MultiSelectionTreeViewItem; if (tvi != null) { if (!tvi.IsExpanded) { tvi.IsExpanded = true; } //如果子项还没有初始化容器,则刷新布局获取容器,保证取遍历的时候取到 if (tvi.ItemContainerGenerator.Status != System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated) { tvi.UpdateLayout(); } tvi.ExpandAllChildren(); } } } } }
private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CheckBoxTreeView cb = (CheckBoxTreeView)d; if (cb._poup != null) { if ((bool)e.NewValue) { cb.Dispatcher.BeginInvoke(DispatcherPriority.Send, (DispatcherOperationCallback) delegate(object arg) { //展开所有子节点 if (cb._treeView != null && cb.ItemsSource != null) { foreach (var item in cb._treeView.Items) { if (item is MultiSelectionTreeViewItem) { (item as MultiSelectionTreeViewItem).IsExpanded = true; } else { MultiSelectionTreeViewItem tvi = cb._treeView.ItemContainerGenerator.ContainerFromItem(item) as MultiSelectionTreeViewItem; if (tvi != null) { if (!tvi.IsExpanded) { tvi.IsExpanded = true; } //如果子项还没有初始化容器,则刷新布局获取容器,保证取遍历的时候取到 if (tvi.ItemContainerGenerator.Status != System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated) { tvi.UpdateLayout(); } tvi.ExpandAllChildren(); } } } } return(null); }, cb); } } }