void dragSource_DragStart(object sender, DragDropStartEventArgs e) { ArrayList draggedItems = new ArrayList(); List <IList> sourceList = new List <IList>(); SelectedRecordCollection selectedRecords = this.dataPresenter.SelectedItems.Records; DataRecord lastSelectedDataRecord = null; foreach (Record r in selectedRecords) { DataRecord dr = r as DataRecord; if (dr != null) { lastSelectedDataRecord = dr; draggedItems.Add(dr.DataItem); sourceList.Add(GetSourceList(dr)); } } DraggingData data = new DraggingData(); data.Items = draggedItems; data.Lists = sourceList; if (lastSelectedDataRecord != null && lastSelectedDataRecord.ParentRecord != null) { data.SourceProperty = lastSelectedDataRecord.RecordManager.Field.Name; } e.Data = data; }
private void OnRecordDragDrop(object sender, DropEventArgs dragInfo) { var result = VisualTreeHelper.HitTest(dragInfo.DropTarget, dragInfo.GetPosition(dragInfo.DropTarget)); var targetElement = Utilities.GetAncestorFromType(result.VisualHit, typeof(DataRecordPresenter), true) as DataRecordPresenter; if (targetElement != null) { DataRecord targetRecord = targetElement.DataRecord; IList targetList = GetSourceList(targetRecord); object targetItem = targetRecord.DataItem; Type targetType = targetItem.GetType(); DraggingData draggedData = dragInfo.Data as DraggingData; if (draggedData != null) { IList itemsList = draggedData.Items; object firstItem = itemsList[0]; IList <IList> listsList = draggedData.Lists; if (!targetType.IsInstanceOfType(firstItem)) { // the target type doesn't match the items we are dropping, get the child list from the parent if we have a property. if (draggedData.SourceProperty != null) { PropertyInfo listProperty = targetType.GetProperty(draggedData.SourceProperty); targetList = listProperty.GetValue(targetItem, null) as IList; if (targetList != null) { for (int i = itemsList.Count - 1; i >= 0; i--) { object currentItem = itemsList[i]; int targetIndex = targetList.IndexOf(targetItem); listsList[i].Remove(currentItem); targetList.Add(currentItem); } } } else { MessageBox.Show("Can't drop here"); } } else { for (int i = itemsList.Count - 1; i >= 0; i--) { object currentItem = itemsList[i]; int targetIndex = targetList.IndexOf(targetItem); listsList[i].Remove(currentItem); targetList.Insert(targetIndex, currentItem); } } } } }