public virtual void SetExpandedWithChildren(TreeViewItem fromItem, bool expand)
{
Stack<TreeViewItem> stack = new Stack<TreeViewItem>();
stack.Push(fromItem);
HashSet<int> other = new HashSet<int>();
while (stack.Count > 0)
{
TreeViewItem item = stack.Pop();
if (item.hasChildren)
{
other.Add(item.id);
foreach (TreeViewItem item2 in item.children)
{
stack.Push(item2);
}
}
}
HashSet<int> source = new HashSet<int>(this.expandedIDs);
if (expand)
{
source.UnionWith(other);
}
else
{
source.ExceptWith(other);
}
this.SetExpandedIDs(source.ToArray<int>());
}