问题描述
我工作的一个WPF应用程序。
I am working on a WPF application.
按规定我要显示的数据网格项的列表。每排也具有删除按钮时,使用这个按钮可以删除相应的项目。
我也希望拖放功能的网格。即用户可以移动的行向上/向下。
As per requirement I want to show list of item in data grid. Each row also having a "DELETE" button, using this button we can deleted the corresponding item.I also want Drag and Drop feature for the Grid. That is user can move the rows up/down.
我使用的PreviewMouseLeftButtonDown
和删除
实施拖放功能DataGrid的事件。
I am using "PreviewMouseLeftButtonDown"
and "Drop"
event of the datagrid to implement the Drag and Drop feature.
有关DELETE键,我已经绑定delete命令。
For DELETE button , I have bind the Delete Command.
Command="{Binding ElementName=viewName,Path=DataContext.DeleteCommand}"
我也试过
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.DeleteCommand}"
现在的问题是,当我点击删除按钮,删除命令处理程序不被解雇。但如果我删除的PreviewMouseLeftButtonDown和放数据网格,删除命令处理程序工作完美的事件。
Now the issue is , when I click the "DELETE" button, the delete command handler not getting fired. But if I remove the "PreviewMouseLeftButtonDown" and "Drop" events of the data grid , the delete command handler working perfectly.
另外我注意到,即使评论均码里面的的PreviewMouseLeftButtonDown后添加的PreviewMouseLeftButtonDown事件,这也阻止删除命令处理程序的执行。
Also I noticed that ,Even if commented all code inside the "PreviewMouseLeftButtonDown" after add the PreviewMouseLeftButtonDown event, it also block the execution of Delete command handler.
<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的代码
Code of 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;
}
我这个问题苦苦挣扎。
I am struggling with this issue.
如果任何一个有一个解决方案,请让我知道。
If any one have a solution, Please let me know.
谢谢,
Ranish
Thanks,Ranish
推荐答案
移动 DragDrop.DoDragDrop
调用DataGrid的的MouseMove
事件:
Move the DragDrop.DoDragDrop
call to the datagrid's MouseMove
event:
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;
}
它不仅解决您的问题,但它提供了一个更好的用户体验。当我移动鼠标,而不是当我按下行的阻力应启动。
Not only does it fix your problem but it provides a better user experience. The drag should be initiated when I move the mouse instead of when I press the row.
下次请链接的 - 这将使它更容易为他人复制,你所遇到的问题。
Next time please link the tutorial that you are using - it will make it much easier for others to reproduce the issue that you are encountering.
这篇关于在DataGrid中按钮不被解雇因的PreviewMouseLeftButtonDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!