上下文菜单在下次单击时显示更改

Context menu shows changes on next click

我正在使用 DevExpress TreeList。我有两种方法 MouseDown()MouseUP() 通过右键单击从树列表中获取项目,然后在运行时显示一个 contextmenu/popup 菜单并对其进行更改。

问题:ContextMenu 或 PopupMenu 在下次单击时显示 barSubItem3.Enabled = false; 更改。不在当前点击上。

private void TreeList1_MouseDown(object sender, MouseEventArgs e)
    {
        TreeList tree = sender as TreeList;
        if (e.Button == MouseButtons.Right && ModifierKeys == Keys.None
            && tree.State == TreeListState.Regular)
        {
            Point pt = tree.PointToClient(MousePosition);
            TreeListHitInfo info = tree.CalcHitInfo(pt);

            if (info.HitInfoType == HitInfoType.Cell)
            {
                SavedFocused = new TreeListNode();
                SavedFocused = tree.FocusedNode;
                tree.FocusedNode = info.Node;

            /* get value from node that is clicked by column index */
                switch (SavedFocused.GetValue(0).ToString())
                {
                    case "A":
                        barSubItem3.Enabled = false;
                        break;
                    case "B":
                        barSubItem3.Enabled = true;
                        break;
                }

            }

        }
    }

private void TreeList1_MouseUp1(object sender, MouseEventArgs e)
    {

        TreeList tree = sender as TreeList;
        if (e.Button == MouseButtons.Right && ModifierKeys == Keys.None
            && tree.State == TreeListState.Regular)
        {

            popUpMenu.ShowPopup(MousePosition);
        }
    }  

我猜这是因为您实际上是在修改已经显示的项目的状态。

改为使用 PopupMenuShowing 事件。下面是一个关于如何使用 GridView 修改 PopUpMenu 的示例。

private void Whatever_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
{
    var menu = e.Menu;
    var hi = e.HitInfo;
    if (!(sender is GridView view)) 
        return;

    var inDetails = (hi.HitTest == GridHitTest.EmptyRow);
    if (menu == null && inDetails)
    {
        menu = new DevExpress.XtraGrid.Menu.GridViewMenu(view);
        e.Menu = menu;
    }
    if (menu == null) 
        return;

    //If there are any entries, show "Duplicate" button
    var rowHandle = hi.RowHandle;
    if (!view.IsDataRow(rowHandle)) return;
    var mnuDuplicate = new DXMenuItem("Duplicate",
        async delegate { await ClickDuplicate(); },
        Properties.Resources.copy_16x16)
    {
        BeginGroup = true
    };
    menu.Items.Add(mnuDuplicate);
}