2014-10-04 60 views
2

我正在WPF应用程序上工作。由于PreviewMouseLeftButtonDown Datagrid中的按钮没有被触发

根据要求,我想显示数据网格中的项目列表。每一行还有一个“DELETE”按钮,使用这个按钮我们可以删除相应的项目。 我也想要为网格拖放功能。这是用户可以向上/向下移动行。

我正在使用“PreviewMouseLeftButtonDown”“Drop” datagrid事件来实现拖放功能。

对于DELETE按钮,我绑定了删除命令。

Command="{Binding ElementName=viewName,Path=DataContext.DeleteCommand}" 

我也试过

Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.DeleteCommand}" 

现在的问题是,当我点击“删除”按钮,删除命令处理程序不被解雇。但是,如果我删除数据网格的“PreviewMouseLeftButtonDown”和“Drop”事件,则删除命令处理程序可以正常工作。

另外我注意到,即使在添加了PreviewMouseLeftButtonDown事件后在“PreviewMouseLeftButtonDown”中添加了所有代码,它也会阻止Delete命令处理程序的执行。

    <DataGridTemplateColumn Width="35" > 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <Button Width="30" Content="X"  Command="{Binding ElementName=viewCSW,Path=DataContext.DeleteCommand}" HorizontalAlignment="Center" Margin="0,0,0,0" /> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 

       </DataGrid.Columns> 
       <DataGrid.RowStyle> 
        <Style TargetType="DataGridRow"> 
         <Setter Property="Height" Value="25"/> 
        </Style> 
       </DataGrid.RowStyle> 

PreviewMousedown

private void dgEmployee_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 

      prevRowIndex = GetDataGridItemCurrentRowIndex(e.GetPosition); 

      if (prevRowIndex < 0) 
       return; 
      dgEmployee.SelectedIndex = prevRowIndex; 

      var selectedEmployee = dgEmployee.Items[prevRowIndex];//as Employee; 

      if (selectedEmployee == null) 
       return; 
      //Now Create a Drag Rectangle with Mouse Drag-Effect 
      //Here you can select the Effect as per your choice 
      DragDropEffects dragdropeffects = DragDropEffects.Move; 

      if (DragDrop.DoDragDrop(dgEmployee, selectedEmployee, dragdropeffects) 
           != DragDropEffects.None) 
      { 
       //Now This Item will be dropped at new location and so the new Selected Item 
       dgEmployee.SelectedItem = selectedEmployee; 
      } 


      //  sourceElement.CaptureMouse(); 
      // return; 

     } 

的代码我这个问题挣扎。

如果有任何人有解决方案,请让我知道。

感谢, Ranish

+0

此处为鼠标放下处理程序的邮政编码。你在那个处理程序中设置了'e.Handled = true'吗? – 2014-10-04 11:41:33

+0

没有。我没有使用“e.Handled = true”。此外,即使我评论了PreviewMouseDown事件中的代码,delete命令也没有被触发。 – Ranish 2014-10-04 11:57:09

+0

在这里添加一些相关的代码,然后复制您的问题。 – 2014-10-04 11:59:43

回答

2

移动DragDrop.DoDragDrop调用DataGrid的MouseMove事件:

private void dgEmployee_MouseMove(object sender, MouseEventArgs e) 
    { 
     if(e.LeftButton == MouseButtonState.Pressed) 
     { 
      Employee selectedEmp = dgEmployee.Items[prevRowIndex] as Employee; 
      if (selectedEmp == null) 
       return; 

      DragDropEffects dragdropeffects = DragDropEffects.Move; 
      if (DragDrop.DoDragDrop(dgEmployee, selectedEmp, dragdropeffects) 
          != DragDropEffects.None) 
      { 
       //Now This Item will be dropped at new location and so the new Selected Item 
       dgEmployee.SelectedItem = selectedEmp; 
      } 
     } 
    } 

更新PreviewMouseLeftButtonDown处理程序:

void dgEmployee_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     prevRowIndex = GetDataGridItemCurrentRowIndex(e.GetPosition); 

     if (prevRowIndex < 0) 
      return; 

     dgEmployee.SelectedIndex = prevRowIndex; 
    } 

它不仅解决您的问题,但它提供更好的用户体验。当我移动鼠标而不是按下该行时,应该启动拖动。

下一次请链接教程you are using - 它会使其他人重现您遇到的问题更容易。

+0

非常感谢你!现在它工作了!好的,下次我会上传示例代码以及我的问题。 – Ranish 2014-10-05 04:51:02