/// <summary> /// 折叠起那些不是当前选择行或其父节点的所有行。 /// </summary> private void ApplyAutoCollapse() { if (!this.AutoCollapse) { return; } object selected = this.SelectedItem; var itemsControl = this.RootItemsControl; if (selected == null) { //if we don't have a selected item, just collapse the //root items foreach (TreeGridRow node in TreeGridHelper.TraverseRows(itemsControl)) { node.IsExpanded = false; } } else { var parents = this.GetParentItemList(selected); foreach (object parent in parents) { var parentNode = this.FindChildRow(parent, itemsControl); if (parentNode == null) { string msg = "Cannot collapse item '{0}' - the item does not exist in the hierarchy of the tree's bound items."; msg = String.Format(msg, GetId(parent)); throw new InvalidOperationException(msg); } foreach (TreeGridRow item in TreeGridHelper.TraverseRows(itemsControl)) { //collapse all items that are no ancestors if (item == parentNode) { continue; } item.IsExpanded = false; } //go a level deeper itemsControl = parentNode; } //finally collapse the item and its siblings foreach (TreeGridRow item in TreeGridHelper.TraverseRows(itemsControl)) { item.IsExpanded = false; } } }
/// <summary> /// 让当前所有行的单元格重新测量。 /// </summary> private void InvalidateCells() { var grid = this.TreeGrid; if (grid != null && grid.IsColumnsVirtualizingEnabled) { var rows = TreeGridHelper.TraverseRows(grid); foreach (var item in rows) { if (item.CellsPanel != null) { item.CellsPanel.InvalidateMeasure(); } } } }