Reputation: 927
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.
I am using “PreviewMouseLeftButtonDown”
and “Drop”
event of the datagrid to implement the Drag and Drop feature.
For DELETE button , I have bind the Delete Command.
Command="{Binding ElementName=viewName,Path=DataContext.DeleteCommand}"
I also tried
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.DeleteCommand}"
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.
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>
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.
Thanks, Ranish
Upvotes: 3
Views: 1257
Reputation: 1234
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;
}
}
}
The updated PreviewMouseLeftButtonDown
handler:
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.
Upvotes: 3