问题
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?
回答1:
This can't freaking be it. FullPath is apparently the path of the node in the treeview, so for example if you have Node2 which is a sub-node of Node1, then the FullPath of Node1 is "Node1" and of Node2 "Node1//Node2" … Now the thing is that FOR SOME WEIRD REASON my mNodeToExpand loses the Information of FullPath and suddenly it's only "Node2" instead of "Node1//Node2"... Because of that it isn't expanded cause the TreeView doesn't find a node with this FullPath… Now what I did is that I stored the FullPath in a string and when I want to expand it I search for the node with this FullPath and then expand what is returned. This is so stupid, cause imagine a list of 1 000 000 nodes. This could take very long. I'm starting to really dislike this Crownwoods.DotNetMagic library but it's already existing Code I'm working on.
来源:https://stackoverflow.com/questions/51519226/treeview-node-not-expanding