2015-09-08 19 views
1

我跟着this tutorial来实现Drag & Drop在我的数据网格上。我根据this link更改了它,以便能够在组之间移动元素。MouseMove事件可以防止ComboBox被“打开”

我的datagrid包含一个按钮的列,所以我跟着this answer使按钮再次可用。我也有3列与组合框,他们不能使用(你可以点击它们,然后他们看起来像组合框,但第二次点击不会扩大它)。

其中两个定义为DataGridComboBoxColumn,一个定义为DataGridTemplateColumnComboBox在标签DataGridTemplateColumn.CellEditingTemplate中。

前两个是这样的:

<DataGridComboBoxColumn Header="Entity" 
    ItemsSource="{StaticResource tl}" 
    DisplayMemberPath="Name" 
    SelectedValuePath="Name" 
    SelectedValueBinding="{Binding Entity}" 
    x:Name="cmbEntity"></DataGridComboBoxColumn> 

数据网格的定义是这样的:

<DataGrid Grid.Row="1" Name="myGrid" IsManipulationEnabled="True" ItemsSource="{Binding Source={StaticResource cvs}}" AutoGenerateColumns="False" 
RowEditEnding="myGrid_RowEditEnding" PreviewKeyDown="myGrid_PreviewKeyDown" SelectedCellsChanged="myGrid_SelectedCellsChanged" 
AllowDrop="True" MouseMove="DataGrid_MouseMove" PreviewMouseLeftButtonDown="DataGrid_PreviewMouseLeftButtonDown" Drop="DataGridView_Drop"> 

并且如上所述,该方法根据教程实现。我已经尝试在事件处理程序中使用e.Handled=false,但它没有帮助(并且它可能无用,因为打开组合框不是事件?)

通过一次删除一个事件处理程序,我发现MouseMove事件的问题,代码如下:

void DataGrid_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.LeftButton == MouseButtonState.Pressed) { 

      Console.Out.WriteLine("MouseButtonState.Pressed"); 

      DataGrid dataGrid = sender as DataGrid; 
      prevRowIndex = GetDataGridItemCurrentRowIndex(dataGrid, e.GetPosition); 

      if (prevRowIndex < 0) { return;} 

      dataGrid.SelectedIndex = prevRowIndex; 
      DefaultValue selectedDV = dataGrid.Items[prevRowIndex] as DefaultValue; 

      if (selectedDV == null) { return; } 

      DragDropEffects dragDropEffects = DragDropEffects.Move; 
      if (DragDrop.DoDragDrop(dataGrid, selectedDV, dragDropEffects) != DragDropEffects.None) 
      { 
       dataGrid.SelectedItem = selectedDV; 
      }  
     } 
    } 

我不完全理解为什么发生这种情况,因为我真的不移动鼠标,我只要按一下含有组合的细胞框。是否有可能同时拥有,拖动&拖放和ComboBoxes?

编辑:我修改从the tutorial我用于显示我有问题的项目:Download from my dropbox 我改变了薪酬列是一个组合框(当然也添加了分组,因为我认为这可能是重要的)

+0

如果你可以发布一个简单的项目来显示这个问题,那很好,否则你需要花费所有的教程来重现你的错误。 – netaholic

+0

好吧,我会这样做,但这需要我一些时间(我无法上传完整的项目,因为它记录了我自己的代码)。 –

+0

那么,它怎么样? – netaholic

回答

0

首先,问题是什么。

即使没有移动鼠标,鼠标移动事件也会被解雇。快速搜索表明,这是出于行为。 然后datagrid会尝试将单击的单元格放入编辑模式,但是鼠标移动事件代码会调用以防止出现组合框。

,我建议的解决办法是做到以下几点:

定义在主窗口:

 Point mousePositionOnButtonPress; 
     double deltaToStartDrag = 10; 

和修改您的MouseMove事件到以下几点:

if (e.LeftButton == MouseButtonState.Pressed) 
      { 
       var position = e.GetPosition(this); 
       if (mousePositionOnButtonPress == new Point()) 
       { 
        mousePositionOnButtonPress = new Point(position.X, position.Y); 
       } 
       else 
        if (Math.Abs(mousePositionOnButtonPress.X - position.X) > deltaToStartDrag || Math.Abs(mousePositionOnButtonPress.Y - position.Y) > deltaToStartDrag) 
        { 
         Console.Out.WriteLine("MouseButtonState.Pressed"); 

         DataGrid dataGrid = sender as DataGrid; 
         prevRowIndex = GetDataGridItemCurrentRowIndex(dataGrid, e.GetPosition); 

         if (prevRowIndex < 0) { return; } 

         dataGrid.SelectedIndex = prevRowIndex; 
         Employee selectedDV = dataGrid.Items[prevRowIndex] as Employee; 

         if (selectedDV == null) { return; } 

         DragDropEffects dragDropEffects = DragDropEffects.Move; 
         if (DragDrop.DoDragDrop(dataGrid, selectedDV, dragDropEffects) != DragDropEffects.None) 
         { 
          dataGrid.SelectedItem = selectedDV; 
         } 
        } 
       // 
      } 

还订阅DataGrid的MouseUp事件如下

private void dgEmployee_MouseUp(object sender, MouseButtonEventArgs e) 
     { 
      mousePositionOnButtonPress = new Point(); 
     } 

这将推迟拖放的开始,直到用户将其鼠标按下至10个像素为止。这是我不得不说的关于你的问题。 我也建议你看看这篇文章http://www.hardcodet.net/2009/03/moving-data-grid-rows-using-drag-and-drop 海事组织它有更多更干净的代码,它看起来更好。 我相信,通过一些调整,你可以使它与群组一起工作。

+0

真酷!我认为它可能是这样的(MouseMove被开除而没有移动),但这对我来说没有意义... –