private void OnNodeExpanded(TreeGridRow treeNode) { //处理 LazyLoading //结点被展开后事件 //完成懒加载时的结点创建工作,并设置各结点的 Filter 属性。 object item = treeNode.DataContext; if (item != null) { //布局 var itemKey = GetId(item); _renderExpansion.Add(itemKey); var nodeItems = treeNode.Items; //懒加载状态下,创建结点。 if (this.IsLazyLoading) { TreeGridHelper.ClearDummyChildNode(treeNode); if (nodeItems.Count == 0) { var childItems = this.GetChildItems(item); treeNode.ItemsSource = childItems; //刷新以排序 if (nodeItems.NeedsRefresh) { nodeItems.Refresh(); } } } if (nodeItems.Count == 0) { //如果结点没有数据,则不需要展开它。 treeNode.IsExpanded = false; _renderExpansion.Remove(itemKey); } } }
/// <summary> /// Updates the tree if items were removed from an observed /// child collection. This might cause rendered tree nodes /// to be removed. In case lazy loading is enabled, the update /// of the UI may be as subtle as to remove an expander from /// a collapsed node if the represented item's childs were /// removed (or the childs that pass an active filter).<br/> /// </summary> /// <param name="observed">The observed collection.</param> /// <param name="e">The event arguments that provide the /// removed items.</param> private void HandleRemovedChildItems(ICollection observed, NotifyCollectionChangedEventArgs e) { IList childs = e.OldItems; if (childs.Count == 0) { return; } //get the node of the parent item that contains the evented childs var parentNode = GetParentNode((object)childs[0], observed); if (parentNode == null) { return; } foreach (object childItem in childs) { //check if we have a corresponding open node //-> not necessarily the case if we're doing lazy loading var childRow = tree.FindChildRow(childItem, parentNode); if (childRow != null) { //unregister listeners UnregisterListeners(childRow); //remove node from UI parentNode.Items.Remove(childRow); } } //in case of lazy loading, the tree might contain a dummy node //(has not been expanded). However, it might be that it's now //completely empty... if (observed.Count == 0) { TreeGridHelper.ClearDummyChildNode(parentNode); parentNode.IsExpanded = false; } }