【问题标题】:After DragDrop.DoDragDrop other controls with in the grids are not able to select(not responding)DragDrop.DoDragDrop 后网格中的其他控件无法选择(不响应)
【发布时间】:2013-06-18 15:21:50
【问题描述】:

我正在使用 xamdatagrid 开发拖放功能。我已经实现了如下所述的拖放功能。

1) 我创建了一个 PreviewMouseLeftButtonDown 事件,在这个事件中我调用了 DragDrop.DoDragDrop() 方法。 2) 我还有一个 MouseMove 事件,我正在从网格中获取选定的行。我在上面的 PreviewMouseLeftButtonDown 事件中使用这个选定的行来创建放置数据。

我的问题是我在同一个网格中有一个下拉列表(组合框)。由于 DragDrop.DoDragDrop() 是一个同步方法,并且它是从 PreviewMouseLeftButtonDown 调用的,所以它不会释放鼠标事件,直到拖放到目标并且下拉选择将无法正常工作。

我也尝试设置布尔标志,但我无法将拖放和控件选择(下拉选择)与 PreviewMouseLeftButtonDown 方法区分开来。仅当我执行拖放而不选择网格行中的下拉菜单时,我才需要调用 DragDrop.DoDragDrop()。如何识别操作是拖放还是下拉选择?

是否有其他方法可以在不等待 DragDrop.DoDragDrop() 结果的情况下启用鼠标事件。

private void PreviewMouseLeftButtonDown (object sender, MouseButtonEventArgs e)
        {
            //record presenter I am setting PreviewMouseLeftButtonDown so I am doing the this
            var grid = (sender as DataRecordCellArea).FieldLayout.DataPresenter as XamDataGrid;
            if (grid != null)
                {
itemName = variableViewModel.OriginalPrimaryKey, variableViewModel.Value, variableViewModel.Variable.GuiDisplayUnits;
var data = new DataObject();
                    data.SetData(DataFormats.StringFormat, itemName);
                    DragDrop.DoDragDrop(grid, data, DragDropEffects.Copy);
                }
        }

//这个用来获取选中的行

private void KeywordMouseMove(object sender, MouseEventArgs e)
 {
    var grid = (sender as DataRecordCellArea).FieldLayout.DataPresenter as XamDataGrid;
            if (grid != null)
            {
                //we have editable and not ediable columns.previously we faced issue with noe editable column drag so this added
                //Cell area will get for editable and presenter will get for non editable fields.This will get the selected row
                var drcellarea = e.Source as DataRecordCellArea;
                var drpresenter = e.Source as CellValuePresenter;

                DataRecord dataRecord = null;
                if (drcellarea != null)
                {
                    dataRecord = drcellarea.DataContext as DataRecord;
                }
                else if (drpresenter != null)
                {
                    dataRecord = drpresenter.DataContext as DataRecord;
                }

                if (dataRecord != null)
                {
                    var selectedItem = dataRecord.DataItem as VariableViewModel;
                    viewmodel.SelectedItem = selectedItem;
                }
            }

  }

【问题讨论】:

    标签: wpf c#-4.0 wpfdatagrid


    【解决方案1】:

    在没有看到任何代码的情况下很难确切地知道问题出在哪里,但请尝试在MouseMove 处理程序中调用DragDrop.DoDragDrop 以查看它是否可以解决您的问题。像这样的:

    编辑:

        private void KeywordMouseMove(object sender, MouseEventArgs e)
        {
            var grid = (sender as DataRecordCellArea).FieldLayout.DataPresenter as XamDataGrid;
            if (grid != null && e.LeftButton == MouseButtonState.Pressed)
            {
                itemName = variableViewModel.OriginalPrimaryKey, variableViewModel.Value, variableViewModel.Variable.GuiDisplayUnits;
                var data = new DataObject();
                data.SetData(DataFormats.StringFormat, itemName);
    
                DragDrop.DoDragDrop(grid, data, DragDropEffects.Copy);
            }
        }
    

    我已经修改了您的 KeyboardMouseMove 处理程序以表明我的意思。这现在包含您现在可以删除的 PreviewMouseLeftButtonDown 处理程序中的代码。我假设您在目标 Element 上有一个 Drop 处理程序,该处理程序将数据对象从 DragDrop 操作中取出并处理它。

    【讨论】:

    • 我有两个场景。当拖放一个 cpolumn 时它很好,它应该执行下面的代码。但是如果我在网格中有一个下拉菜单,那么相同的代码将执行,因为我们正在使用异步 DragDrop.DoDragdrop() 它不会在下拉列表中给出所选项目。我应该只在拖动时调用 DoDragdrop。不应该调用下拉选择。
    • 能贴出相关代码吗? (好吧,我打字的时候就弹出来了)
    • 我有两种情况。在拖放网格列/行时,调用 DoDragdrop 很好,但在从网格列中选择下拉列表时不应调用。如果它调用 DoDragdrop 那么下拉选择将不起作用,因为 DragDrop.DoDragdrop() 是一个同步调用,它不会离开鼠标事件,直到放下对象。
    • 如果您在 MouseMove 处理程序中开始拖放并检查是否按下左键,当从 Grid 列选择下拉菜单时它不会触发,但仍可用于拖动网格/列.
    • 所以你是说我需要打电话给你评论中提到的 dodragdropas 对吗?
    猜你喜欢
    • 1970-01-01
    • 2011-12-23
    • 2013-07-03
    • 2021-08-06
    • 2014-06-12
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多