Ich habe eine TreeView und abonniert die "TreeViewConnections_AfterExpand" und "" -Ereignis. Jeder TreeNode enthält MenuScript-Ereignisse. und der folgende Code:TreeView AfterExpand Event funktioniert manchmal nicht C#
//event
private void TreeViewConnections_AfterExpand(object sender, TreeViewEventArgs e)
{
var activeKey = e.Node.ImageKey.Replace("Inactive", "Active");
e.Node.ImageKey = activeKey;
e.Node.SelectedImageKey = activeKey;
}
//event
private void TreeViewConnections_MouseClick(object sender, MouseEventArgs e)
{
var currentNode = this.treeViewConnections.GetNodeAt(e.Location);
if (currentNode == null) return;
var currentBounds = currentNode.Bounds;
Rectangle bounds = new Rectangle(currentBounds.Left - ExpandIcon.Width, currentBounds.Y, currentBounds.Width - 5, currentBounds.Height);
if (bounds.Contains(e.Location)) return;
this.treeViewConnections.SelectedNode = currentNode;
if (e.Button == MouseButtons.Right)
{
SetupConnectionMenus(currentNode);
}
}
private void SetupConnectionMenus(TreeNode node)
{
var isOpened = node.Nodes.Count > 0;
if (node.ContextMenu == null)
{
var menu = new ContextMenuStrip();
menu.Items.AddEx("Open Connection", node.Name + "_Open", !isOpened, onClick: OpenConnection_Click, context: node);
menu.Items.AddEx("Close Connection", node.Name + "_Close", isOpened, onClick: CloseConnection_Click, context: node);
node.ContextMenuStrip = menu;
}
}
//event
private void OpenConnection_Click(object sender, EventArgs e)
{
var menuItem = sender as ToolStripMenuItem;
var currentNode = menuItem.Tag as TreeNode;
OpenConnection(currentNode);
}
//event
private void CloseConnection_Click(object sender, EventArgs e)
{
var menuItem = sender as ToolStripMenuItem;
var currentNode = menuItem.Tag as TreeNode;
currentNode.Nodes.Clear();
currentNode.Collapse();
}
private void OpenConnection(TreeNode node)
{
treeViewConnections.BeginUpdate();
//add child node to the node.
treeViewConnections.EndUpdate();
node.Expand(); //?????
}
TreeViewConnections_AfterExpand Ereignis irgendwann funktioniert nicht. wie in der Abbildung dargestellt:
Aber in diesem Fall ist es etwas anderes brauche ich?
Wird node.Expand() jedes Mal aufgerufen, wenn Sie erwarten? – Reniuz
ja. Jedes Mal, wenn ich es anrufe, hoffe ich, dass es untergeordnete Knoten erweitert und gleichzeitig das Knotensymbol –
chang Knoten-Symbol im Ereignis "TreeViewConnections_AfterExpand" ändert. –