我正在使用Gongsolution框架来处理拖放功能。但是我有一个问题。当我取消选择某个项目并将其拖放到另一个项目时,被删除的项目就消失了。当我选择其他项目时出现一个项目。预先谢谢你。
处理拖放功能的代码:
public static bool CanAcceptData(IDropInfo dropInfo)
{
if (dropInfo == null || dropInfo.DragInfo == null)
{
return false;
}
if (!dropInfo.IsSameDragDropContextAsSource)
{
return false;
}
// do not drop on itself
var isTreeViewItem = dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.TargetItemCenter)
&& dropInfo.VisualTargetItem is TreeViewItem;
if (isTreeViewItem && dropInfo.VisualTargetItem == dropInfo.DragInfo.VisualSourceItem)
{
return false;
}
if (dropInfo.DragInfo.SourceCollection == dropInfo.TargetCollection)
{
var targetList = dropInfo.TargetCollection.TryGetList();
return targetList != null;
}
// else if (dropInfo.DragInfo.SourceCollection is ItemCollection) {
// return false;
// }
else if (dropInfo.TargetCollection == null)
{
return false;
}
else
{
if (TestCompatibleTypes(dropInfo.TargetCollection, dropInfo.Data))
{
var isChildOf = IsChildOf(dropInfo.VisualTargetItem, dropInfo.DragInfo.VisualSourceItem);
return !isChildOf;
}
else
{
return false;
}
}
}
public static IEnumerable ExtractData(object data)
{
if (data is IEnumerable && !(data is string))
{
return (IEnumerable)data;
}
return Enumerable.Repeat(data, 1);
}
public static void SelectDroppedItems(IDropInfo dropInfo, IEnumerable items, bool applyTemplate = true, bool focusVisualTarget = true)
{
if (dropInfo == null) throw new ArgumentNullException(nameof(dropInfo));
if (items == null) throw new ArgumentNullException(nameof(items));
var itemsControl = dropInfo.VisualTarget as ItemsControl;
if (itemsControl != null)
{
var tvItem = dropInfo.VisualTargetItem as TreeViewItem;
var tvItemIsExpanded = tvItem != null && tvItem.HasHeader && tvItem.HasItems && tvItem.IsExpanded;
var itemsParent = tvItemIsExpanded ? tvItem : (dropInfo.VisualTargetItem != null ? ItemsControl.ItemsControlFromItemContainer(dropInfo.VisualTargetItem) : itemsControl);
itemsParent = itemsParent ?? itemsControl;
itemsParent.ClearSelectedItems();
foreach (var obj in items)
{
if (applyTemplate)
{
// call ApplyTemplate for TabItem in TabControl to avoid this error:
//
// System.Windows.Data Error: 4 : Cannot find source for binding with reference
var container = itemsParent.ItemContainerGenerator.ContainerFromItem(obj) as FrameworkElement;
container?.ApplyTemplate();
}
itemsParent.SetItemSelected(obj, true);
}
if (focusVisualTarget)
{
itemsControl.Focus();
}
}
}
public static bool ShouldCopyData(IDropInfo dropInfo)
{
// default should always the move action/effect
if (dropInfo == null || dropInfo.DragInfo == null)
{
return false;
}
var copyData = ((dropInfo.DragInfo.DragDropCopyKeyState != default(DragDropKeyStates)) && dropInfo.KeyStates.HasFlag(dropInfo.DragInfo.DragDropCopyKeyState))
|| dropInfo.DragInfo.DragDropCopyKeyState.HasFlag(DragDropKeyStates.LeftMouseButton);
copyData = copyData
//&& (dropInfo.DragInfo.VisualSource != dropInfo.VisualTarget)
&& !(dropInfo.DragInfo.SourceItem is HeaderedContentControl)
&& !(dropInfo.DragInfo.SourceItem is HeaderedItemsControl)
&& !(dropInfo.DragInfo.SourceItem is ListBoxItem);
return copyData;
}
public virtual void DragOver(IDropInfo dropInfo)
{
if (CanAcceptData(dropInfo))
{
dropInfo.Effects = ShouldCopyData(dropInfo) ? DragDropEffects.Copy : DragDropEffects.Move;
var isTreeViewItem = dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.TargetItemCenter) && dropInfo.VisualTargetItem is TreeViewItem;
dropInfo.DropTargetAdorner = isTreeViewItem ? DropTargetAdorners.Highlight : DropTargetAdorners.Insert;
}
}
public virtual void Drop(IDropInfo dropInfo)
{
if (dropInfo == null || dropInfo.DragInfo == null)
{
return;
}
var insertIndex = dropInfo.UnfilteredInsertIndex;
var itemsControl = dropInfo.VisualTarget as ItemsControl;
if (itemsControl != null)
{
var editableItems = itemsControl.Items as IEditableCollectionView;
if (editableItems != null)
{
var newItemPlaceholderPosition = editableItems.NewItemPlaceholderPosition;
if (newItemPlaceholderPosition == NewItemPlaceholderPosition.AtBeginning && insertIndex == 0)
{
++insertIndex;
}
else if (newItemPlaceholderPosition == NewItemPlaceholderPosition.AtEnd && insertIndex == itemsControl.Items.Count)
{
--insertIndex;
}
}
}
var destinationList = dropInfo.TargetCollection.TryGetList();
var data = ExtractData(dropInfo.Data).OfType<object>().ToList();
var copyData = ShouldCopyData(dropInfo);
if (!copyData)
{
var sourceList = dropInfo.DragInfo.SourceCollection.TryGetList();
if (sourceList != null)
{
foreach (var o in data)
{
var index = sourceList.IndexOf(o);
if (index != -1)
{
sourceList.RemoveAt(index);
// so, is the source list the destination list too ?
if (destinationList != null && Equals(sourceList, destinationList) && index < insertIndex)
{
--insertIndex;
}
}
}
}
}
if (destinationList != null)
{
var objects2Insert = new List<object>();
// check for cloning
var cloneData = dropInfo.Effects.HasFlag(DragDropEffects.Copy) || dropInfo.Effects.HasFlag(DragDropEffects.Link);
foreach (var o in data)
{
var obj2Insert = o;
if (cloneData)
{
var cloneable = o as ICloneable;
if (cloneable != null)
{
obj2Insert = cloneable.Clone();
}
}
objects2Insert.Add(obj2Insert);
destinationList.Insert(insertIndex++, obj2Insert);
}
// Robimy drag and drop i odznaczamy zaznaczony element (nie odznaczal sie) - naprawione
IndexesOfSelectedEmployees.Clear();
IndexesOfSelectedEmployees.Add(--insertIndex);
var selectDroppedItems = itemsControl is TabControl || (itemsControl != null && GongSolutions.Wpf.DragDrop.DragDrop.GetSelectDroppedItems(itemsControl));
if (selectDroppedItems)
{
SelectDroppedItems(dropInfo, objects2Insert);
}
}
}
protected static bool IsChildOf(UIElement targetItem, UIElement sourceItem)
{
var parent = ItemsControl.ItemsControlFromItemContainer(targetItem);
while (parent != null)
{
if (parent == sourceItem)
{
return true;
}
parent = ItemsControl.ItemsControlFromItemContainer(parent);
}
return false;
}
protected static bool TestCompatibleTypes(IEnumerable target, object data)
{
TypeFilter filter = (t, o) => { return (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>)); };
var enumerableInterfaces = target.GetType().FindInterfaces(filter, null);
var enumerableTypes = from i in enumerableInterfaces
select i.GetGenericArguments().Single();
if (enumerableTypes.Count() > 0)
{
var dataType = TypeUtilities.GetCommonBaseClass(ExtractData(data));
return enumerableTypes.Any(t => t.IsAssignableFrom(dataType));
}
else
{
return target is IList;
}
}
public static readonly DependencyProperty SelectDroppedItemsProperty;
public static bool GetSelectDroppedItems(UIElement target)
{
return (bool)target.GetValue(SelectDroppedItemsProperty);
}
用于选择和取消选择项目的代码:
public void ClickMethod(object parameter)
{
List<Employee> selection = ((IList)parameter).Cast<Employee>().ToList();
if (Keyboard.IsKeyDown(Key.LeftCtrl))
{
if (SelectedEmployee == null)
{
IndexesOfSelectedEmployees.Clear();
CountOfSelectedEmployees = 0;
DynamicSearchEmployeeName = String.Empty;
DynamicSearchEmployeeID = String.Empty;
DynamicSearchEmployeeSalary = String.Empty;
DynamicSearchEmployeeDesigner = String.Empty;
DynamicSearchEmployeeEmailID = String.Empty;
}
if (SelectedEmployee != null)
{
foreach (Employee item in Employees)
{
if (item.IsSelected == true)
{
IndexesOfSelectedEmployees.Add(Employees.IndexOf(item));
}
if (item.IsSelected == false)
{
IndexesOfSelectedEmployees.Remove(Employees.IndexOf(item));
}
}
SelectedEmployee = Employees[IndexesOfSelectedEmployees.Last()];
//SelectedEmployee = selection.Last();
foreach (int itemIndexesOfSelectedEmployees in IndexesOfSelectedEmployees.Reverse())
{
foreach (Employee itemEmployees in Employees)
{
if (itemIndexesOfSelectedEmployees == Employees.IndexOf(itemEmployees))
{
itemEmployees.IsSelected = true;
}
}
}
CountOfSelectedEmployees = selection.Count;
}
}
else if (!IndexesOfSelectedEmployees.Any())
{
IndexesOfSelectedEmployees.Clear();
IndexesOfSelectedEmployees.Add(SelectedIndex.GetValueOrDefault());
CountOfSelectedEmployees = selection.Count;
}
else if (IndexesOfSelectedEmployees.Any())
{
if (IndexesOfSelectedEmployees.Count == 1 && IndexesOfSelectedEmployees.Contains((int)SelectedIndex))
{
//IndexesOfSelectedEmployees.Add(Employees.IndexOf(SelectedEmployee));
//SelectedEmployee.IsSelected = false;
SelectedEmployee = null;
CountOfSelectedEmployees = 0;
IndexesOfSelectedEmployees.Clear();
DynamicSearchEmployeeName = String.Empty;
DynamicSearchEmployeeID = String.Empty;
DynamicSearchEmployeeSalary = String.Empty;
DynamicSearchEmployeeDesigner = String.Empty;
DynamicSearchEmployeeEmailID = String.Empty;
}
else if (IndexesOfSelectedEmployees.Count == 1 && !IndexesOfSelectedEmployees.Contains((int)SelectedIndex))
{
IndexesOfSelectedEmployees.Clear();
IndexesOfSelectedEmployees.Add(SelectedIndex.GetValueOrDefault());
CountOfSelectedEmployees = 1;
}
else if (IndexesOfSelectedEmployees.Count != 1)
{
IndexesOfSelectedEmployees.Clear();
IndexesOfSelectedEmployees.Add(SelectedIndex.GetValueOrDefault());
SelectedEmployee = Employees[IndexesOfSelectedEmployees.Last()];
foreach (int itemIndexesOfSelectedEmployees in IndexesOfSelectedEmployees)
{
foreach (Employee itemEmployees in Employees)
{
if (itemIndexesOfSelectedEmployees == Employees.IndexOf(itemEmployees))
{
itemEmployees.IsSelected = true;
}
if (itemIndexesOfSelectedEmployees != Employees.IndexOf(itemEmployees))
{
itemEmployees.IsSelected = false;
}
}
}
CountOfSelectedEmployees = selection.Count;
}
}
}
public void OnSelectionChanged(object parameter)
{
IList selection = (IList)parameter;
if ((Keyboard.IsKeyDown(Key.LeftCtrl) && (Keyboard.IsKeyDown(Key.A))) == true || (Keyboard.IsKeyDown(Key.LeftShift) && Keyboard.IsKeyDown(Key.Up)) || (Keyboard.IsKeyDown(Key.LeftShift) && Keyboard.IsKeyDown(Key.Down)))
{
CountOfSelectedEmployees = selection.Count;
foreach (Employee item in selection)
{
IndexesOfSelectedEmployees.Add(Employees.IndexOf(item));
}
foreach (int itemIndexesOfSelectedEmployees in IndexesOfSelectedEmployees)
{
foreach (Employee itemEmployees in Employees)
{
if (itemIndexesOfSelectedEmployees == Employees.IndexOf(itemEmployees))
{
itemEmployees.IsSelected = true;
}
}
}
}
}