I have a TreeView with several nodes and if a special node (you will see in code) is deleted, the parent node should be expanded after updating the TreeView.
Here's how I tried it:
public void Remove(){
...
...
else if ((NodeType)n.Tag == NodeType.Attribute) //Here I simply check if it's the "special" parent
{
Commands.CommandAttributeRemove cmd = (Commands.CommandAttributeRemove)mAppData.CommandFactory.Create("AttributeRemove");
cmd.Data = n.Text;
cmd.ObjectClass = mObjectClass;
cmd.ObjectTypeName = n.Parent.Parent.Text;
list.Add(cmd);
mNodeToExpand = mTreeView.SelectedNode.Parent; //THIS LINE IS IMPORTANT... mNodeToExpand is a member variable which I use in UpdateData()
}
...
...
UpdateData();
}
public void UpdateData()
{
… //A lot of not so important stuff happening here (at least not important for what I want, I think)
...
//Update Selected Items (for the case that objects were deleted) and UpdateSelection
OnSelect();
//UpdateSelection();
this.Update();
Now that's interesting stuff:
if (mNodeToExpand != null)
{
mNodeToExpand.Expand();
mNodeToExpand = null;
}
}
This is how I tried to achieve what I want, but the node doesn't expand (it still has other children).
In the Remove()
I also tried mTreeView.SelectedNode.Parent.Nodes.Add(new Node("Blabla"));
but it doesn't even add a node.
And in the if(mNodeToExpand!=null)
I also try to set the selectedNode to mNodeToExpand, but it gives me a NullReferenceException EVEN THOUGH I CHECK IF IT'S NULL IN THE IF. WHY?