wenxiangchao
3 天以前 b544d5cccd76735e034c8055918a50927b437474
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using GongSolutions.Wpf.DragDrop;
using NationalInstruments.TestStand.Interop.API;
using Plugin.SeqEditor.Entity;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
 
namespace Plugin.SeqEditor.ViewModel
{
    public class TreeListViewHandler : DefaultDropHandler
    {
        private SeqEditorViewModel model;
        public TreeListViewHandler(SeqEditorViewModel model)
        {
            this.model = model;
        }
 
        public bool CanConvert(IDropInfo dropInfo)
        {
            if (!(dropInfo.TargetItem is StepUI target))
            {
                return false;
            }
 
            if (!(dropInfo.Data is StepUI source))
            {
                return false;
            }
 
            if (source == target)
            {
                return false;
            }
 
            if (target.sequence != source.sequence)
            {
                return false;
            }
            return true;
        }
 
        public override void DragOver(IDropInfo dropInfo)
        {
            if (CanConvert(dropInfo))
            {
                dropInfo.Effects = DragDropEffects.Move;
 
                if (dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.TargetItemCenter))
                {
                    dropInfo.DropTargetAdorner = null;
                }
                else
                {
                    dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
                }
 
                //if (dropInfo.DropTargetAdorner == DropTargetAdorners.Highlight) { 
 
                //}
                //dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
                dropInfo.DropTargetHintState = DropHintState.Active;
                dropInfo.DropTargetHintAdorner = DropTargetAdorners.Hint;
            }
            else
            {
                dropInfo.Effects = DragDropEffects.None;
                dropInfo.DropTargetHintAdorner = DropTargetAdorners.Hint;
                dropInfo.DropTargetHintState = DropHintState.Error;
            }
        }
 
        public override void Drop(IDropInfo dropInfo)
        {
            if (!CanConvert(dropInfo))
            {
                return;
            }
            //var sourceItem = (TreeItemData)dropInfo.Data;
            //var targetItem = (TreeItemData)dropInfo.TargetItem;
 
            var source = dropInfo.Data as StepUI;
            var target = dropInfo.TargetItem as StepUI;
 
            int sourceIndex = source.IndexInSequence;
            int targetIndex = target.IndexInSequence;
 
 
            if (sourceIndex == targetIndex)
            {
                return;
            }
 
            bool insertBefore = dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.BeforeTargetItem);
 
            if (!insertBefore && (sourceIndex == targetIndex + 1)) {
                return;
            }
 
            ObservableCollection<StepUI> datasource = model.sequenceCache[source.sequence];
            int uiSourceIndex = datasource.IndexOf(source);
            int uiTargetIndex = datasource.IndexOf(target);
 
            StepGroups group = target.StepGroup;
 
            if (sourceIndex < targetIndex)
            {
                source.sequence.DeleteStep(sourceIndex, source.StepGroup);
                target.sequence.InsertStep(source.step, targetIndex, group);
                datasource.Move(uiSourceIndex, uiTargetIndex);
            }
            else if (sourceIndex > targetIndex)
            {
                source.sequence.DeleteStep(sourceIndex, source.StepGroup);
                target.sequence.InsertStep(source.step, targetIndex, group);
                datasource.Move(uiSourceIndex, uiTargetIndex);
            }
        }
    }
}