【问题标题】:RadContextMenu.DropDownOpened called before RadGridView.CurrentRow is changedRadContextMenu.DropDownOpened 在 RadGridView.CurrentRow 更改之前调用
【发布时间】:2015-12-15 13:31:58
【问题描述】:

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

问题是当我直接右键单击一行打开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;
}

【问题讨论】:

标签: c# winforms gridview contextmenu dropdown


【解决方案1】:

使用dataGridView.EndEdit();这个函数提交和结束对当前正在编辑的单元格的编辑操作。

更多信息here

【讨论】:

    【解决方案2】:

    很抱歉没有说明我使用的是 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;
        }
    

    现在它按预期工作了。感谢您花时间帮助我:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 2013-08-13
      • 1970-01-01
      相关资源
      最近更新 更多