public void SetModel(ITreeModel model, bool expandRoot)
{
if (_treeView.Model == null)
expandRoot = true;
string selectedTreePath = null;
Dictionary<string, bool> collapsing = new Dictionary<string, bool>();
if (this.SelectedNode != null)
selectedTreePath = this.SelectedNode.TreePath;
if (expandRoot == false)
{
foreach (TreeNodeAdv node in _treeView.AllNodes)
{
BaseNode tag = node.Tag as BaseNode;
if (tag != null)
{
try
{
if (node.IsLeaf == false)
collapsing.Add(tag.TreePath, node.IsExpanded);
}
catch
{
}
}
}
}
_treeView.Model = model;
if (expandRoot)
ExpandRootNode();
else
{
TreeNodeAdv selectedNode = null;
_treeView.SuspendLayout();
foreach (TreeNodeAdv node in _treeView.AllNodes)
{
BaseNode tag = node.Tag as BaseNode;
if (tag != null)
{
if (selectedTreePath != null)
{
if (selectedTreePath == tag.TreePath)
{
selectedNode = node;
}
}
bool isExpanded;
if (collapsing.TryGetValue(tag.TreePath, out isExpanded))
node.IsExpanded = isExpanded;
}
}
if (selectedNode != null)
_treeView.SelectedNode = selectedNode;
else
ExpandRootNode();
_treeView.ResumeLayout();
}
}