WPF Datagrid drag and drop questions

I have Datagrid WPF and I am implementing drag and drop functions.
There is a list of β€œfiles” in the datagrid, and the user can drag them and copy the file to the desktop.
This is done as follows:

string[] files = new String[myDataGrid.SelectedItems.Count]; int ix = 0; foreach (object nextSel in myDataGrid.SelectedItems) { files[ix] = ((Song)nextSel).FileLocation; ++ix; } string dataFormat = DataFormats.FileDrop; DataObject dataObject = new DataObject(dataFormat, files); DragDrop.DoDragDrop(this.myDataGrid, dataObject, DragDropEffects.Copy); 

I have two questions:
1. When I want to drag a few elements - this is a problem, because after I select a pair and start clicking on it to start the drag, which will be selected, and the rest of the elements will be canceled. I tried the solution given here , but for some reason it does not work.
2. I want to remove the dragged item from the datagrid after copying it. The problem is that I don’t know how to check if the file was copied or the user simply dragged it onto the screen without copying it.

Hope you can help me solve these problems. Thanks!

+4
source share
2 answers

I think this is what you are looking for:

add this code to the DataGrid__PreviewMouseLeftButtonDown event handler:

 private void DataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.startingPosition = e.GetPosition(null); DependencyObject dep = (DependencyObject)e.OriginalSource; // iteratively traverse the visual tree until get a row or null while ((dep != null) && !(dep is DataGridRow)) { dep = VisualTreeHelper.GetParent(dep); } //if this is a row (item) if (dep is DataGridRow) { //if the pointed item is already selected do not reselect it, so the previous multi-selection will remain if (songListDB.SelectedItems.Contains((dep as DataGridRow).Item)) { // now the drag will drag all selected files e.Handled = true; } } } 

and now casting will not change your choice.

Good luck

I used the article to write my answer

+5
source

Improved when searching for a string. Also choosing clicking a row when not dragging. Now it behaves just like other Microsoft selectors (e.g. Outlook)

  public TreeDataGrid() { Loaded += TreeDataGrid_Loaded; LoadingRow += new EventHandler<DataGridRowEventArgs>(TreeDataGrid_LoadingRow); } #region MultiSelect Drag object toSelectItemOnMouseLeftButtonUp; void TreeDataGrid_LoadingRow(object sender, DataGridRowEventArgs e) { e.Row.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(Row_PreviewMouseLeftButtonDown); e.Row.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(Row_PreviewMouseLeftButtonUp); } void Row_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DataGridRow row = (DataGridRow)sender; toSelectItemOnMouseLeftButtonUp = null; // always clear selecteditem if (SelectedItems.Contains(row.Item)) //if the pointed item is already selected do not reselect it, so the previous multi-selection will remain { e.Handled = true; // this prevents the multiselection from disappearing, BUT datagridcell still gets the event and sets DataGrid private member _selectionAnchor toSelectItemOnMouseLeftButtonUp = row.Item; // store our item to select on MouseLeftButtonUp } } void Row_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { DataGridRow row = (DataGridRow)sender; if (row.Item == toSelectItemOnMouseLeftButtonUp) // check if it set and concerning the same row { if (SelectedItem == toSelectItemOnMouseLeftButtonUp) SelectedItem = null; // if the item is already selected whe need to trigger a change SelectedItem = toSelectItemOnMouseLeftButtonUp; // this will clear the multi selection, and only select the item we pressed down on typeof(DataGrid).GetField("_selectionAnchor", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this, new DataGridCellInfo(row.Item, ColumnFromDisplayIndex(0))); // we need to set this anchor for when we select by pressing shift key toSelectItemOnMouseLeftButtonUp = null; // handled } } #endregion 
+1
source

All Articles