3
我有一個TreeView和簽署了「TreeViewConnections_AfterExpand」和「」事件。每個人TreeNode都包含MenuScript事件。和下面的代碼:的TreeView AfterExpand事件somtimes不起作用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事件有時不工作。如圖所示:
但是在這種情況下,有沒有別的東西,我需要做什麼?
Node.Expand()是否每次你期望被調用? – Reniuz
是的。每次調用它,我希望它擴展子節點,並在同一時間更改「TreeViewConnections_AfterExpand」事件的節點圖標 –
昌節點圖標。 –