通过MouseDown事件来获取节点,通过节点判断来确定TreeList控件的菜单。
/// <summary>
/// 通过MouseDown事件,判断节点属性,切换右键菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trvTemplate_MouseDown(object sender, MouseEventArgs e)
{
Point p = new Point(Cursor.Position.X, Cursor.Position.Y);
TreeList tree = (TreeList )sender;
TreeListHitInfo hitInfo = tree.CalcHitInfo(e.Location);
if (hitInfo.HitInfoType != HitInfoType.Cell) return;
tree.SetFocusedNode(hitInfo.Node);
TreeListNode node = tree.FocusedNode;
if (node != null && node.Level == 1)
{
tree.ContextMenuStrip = this.contextMenuStrip1;
}
else
{
tree.ContextMenuStrip = null;
}
}