punker76 / gong-wpf-dragdrop

The GongSolutions.WPF.DragDrop library is a drag'n'drop framework for WPF

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DropInfo.InsertPosition Evaluation Issue

pschimmel opened this issue · comments

Hello!

I'm not sure if this is a bug of your library.
I have a TreeView with several hierarchical items. If I drop a item after the last item on the same level it should insert the item at the end of the list of subitems on this level.
However somehow the DropInfo.InsertPosition is evaluated wrong.
As you can see on the following screenshot the value of dropInfo.InsertPosition is AfterTargetItem as it should be. However it is still evaluated as BeforeTargetItem and the first line after the if is executed.

vs

I also tried Enum.HasFlag with the same result.
Is there something I'm missing?

@pschimmel With which version does this happen?

I'm using version 0.1.4.3 (.NET 4)

@pschimmel so can you test the latest source? also avaialble via pre nuget. thx!

I updated to version 1.0.0 Alpha 011 but still no luck.
I was able to reproduce the issue in your DefaultsExample, however it seems that I cannot upload a zip file as an attachment to a post.

The changes I made are rather simple, maybe you could reproduce them in your environment (sorry for the sloppy programming style):

I changed the XAML of the MainWindow under the "Bound TreeViews" tab as follows:

 <TreeView Grid.Column="0"
                  Grid.Row="1"
                  ItemsSource="{Binding TreeCollection}"
                  dd:DragDrop.IsDragSource="True"
                  dd:DragDrop.IsDropTarget="True"
                  dd:DragDrop.DropHandler="{Binding}"
                  dd:DragDrop.UseDefaultDragAdorner="True">

Then I changed the Data class:

    void IDropTarget.DragOver(IDropInfo dropInfo)
    {
      DragDrop.DefaultDropHandler.DragOver(dropInfo);
      if ((dropInfo.Data is CustomDataModel) || dropInfo.TargetGroup == null) {
        //dropInfo.Effects = System.Windows.DragDropEffects.None;
      }
    }

    void IDropTarget.Drop(IDropInfo dropInfo)
    {
      DragDrop.DefaultDropHandler.Drop(dropInfo);
      if (dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.BeforeTargetItem))
        MessageBox.Show("B-4"); // This is shown if example fails
      var data = DefaultDropHandler.ExtractData(dropInfo.Data).OfType<GroupedItem>().ToList();
      foreach (var groupedItem in data) {
        groupedItem.Group = dropInfo.TargetGroup.Name.ToString();
      }
      if (dropInfo.TargetCollection is ICollectionView) {
        ((ICollectionView)dropInfo.TargetCollection).Refresh();
      }
    }

If you drag Item 0 and drop it after Item 9 a MessageBox is shown although it should not have been.

Thats because RelativeInsertPosition should be changed. Check issue #60

You can test it:

        private void Test(RelativeInsertPosition value, RelativeInsertPosition has, bool expected)
        {
            System.Diagnostics.Debug.WriteLine("Value = {0}; Expected = {1}", value.HasFlag(has), expected);
        }

        private void TestRelativeInsertPosition()
        {
            Test(RelativeInsertPosition.BeforeTargetItem, RelativeInsertPosition.BeforeTargetItem, true);
            Test(RelativeInsertPosition.AfterTargetItem, RelativeInsertPosition.BeforeTargetItem, false);
            Test(RelativeInsertPosition.TargetItemCenter, RelativeInsertPosition.BeforeTargetItem, false);

            Test(RelativeInsertPosition.BeforeTargetItem | RelativeInsertPosition.AfterTargetItem, RelativeInsertPosition.BeforeTargetItem, true);
            Test(RelativeInsertPosition.BeforeTargetItem | RelativeInsertPosition.TargetItemCenter, RelativeInsertPosition.BeforeTargetItem, true);
            Test(RelativeInsertPosition.AfterTargetItem | RelativeInsertPosition.TargetItemCenter, RelativeInsertPosition.BeforeTargetItem, false);

            Test(RelativeInsertPosition.BeforeTargetItem | RelativeInsertPosition.AfterTargetItem | RelativeInsertPosition.TargetItemCenter, RelativeInsertPosition.BeforeTargetItem, true);
        }

And output:

Value = True; Expected = True
Value = True; Expected = False
Value = True; Expected = False
Value = True; Expected = True
Value = True; Expected = True
Value = True; Expected = False
Value = True; Expected = True