RadGridView.CurrentRow更改之前调用的RadContextMenu.DropDownOpened

时间:2015-09-18 11:50:52

标签: c# winforms gridview contextmenu dropdown

我正在尝试更改RadContextMenu上的项目,具体取决于我RadGridView中当前选定的行(编辑:OrderList)。如果当前行中的数据绑定项具有正确的属性值,我希望启用该项。

问题在于,当我直接右键单击某行以打开RadContextmenu时,CurrentRow尚未更新,因此使用旧行调用DropDownOpened。 如果我左键单击或双击右键,它可以正常工作。

以下是一些代码:

OrderMenu.DropDownOpened += OrderMenu_DropDownOpened;

方法

private void OrderMenu_DropDownOpened(object sender, EventArgs e)
{
    GoToParentOrderBtn.Enabled = GetSelectedOrder()?.ParentOrderId != null;
}

private OrderViewModel GetSelectedOrder()
{
    return (OrderViewModel)OrderList.CurrentRow.DataBoundItem;
}

1 个答案:

答案 0 :(得分:0)

很抱歉没有指定我正在使用radgridview。

我找到了related answer帮我解决了问题。 我最终做了一个扩展(所以我可以在应用程序周围使用它)到RadGridView,它在mousedown上激活了一个事件:

public partial class RadExtendedGridViewController : RadGridView
{
    public RadExtendedGridViewController()
    {
        InitializeComponent();
        base.MouseDown += RadExtendedGridViewController_MouseDown;
    }

    private void RadExtendedGridViewController_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            var element = this.ElementTree.GetElementAtPoint(e.Location);
            GridDataCellElement cell = element as GridDataCellElement;
            if (cell?.RowElement is GridDataRowElement)
            {
                Rows[cell.RowIndex].IsSelected = true;
            }
        }
    }
}

然后我将GetSelectedOrder更改为使用SelectedRows而不是Current:

    private OrderViewModel GetSelectedOrder()
    {
        return (OrderViewModel)OrderList.SelectedRows.FirstOrDefault()?.DataBoundItem;
    }

现在它按预期工作。感谢您抽出宝贵时间帮助我: - )