1
1
using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
- using System . Text ;
5
- using System . Threading . Tasks ;
2
+ using System . Collections ;
6
3
using System . Windows ;
7
4
using System . Windows . Controls ;
8
- using System . Windows . Data ;
9
- using System . Windows . Documents ;
10
5
using System . Windows . Input ;
11
6
using System . Windows . Media ;
12
- using System . Windows . Media . Imaging ;
13
- using System . Windows . Shapes ;
14
7
15
8
namespace OpenrecYoyakuPlugin
16
9
{
@@ -23,5 +16,111 @@ public SettingsView()
23
16
{
24
17
InitializeComponent ( ) ;
25
18
}
19
+
20
+ private bool IsTheMouseOnTargetRow ( Visual target , GetDragDropPosition pos )
21
+ {
22
+ try
23
+ {
24
+ var posBounds = VisualTreeHelper . GetDescendantBounds ( target ) ;
25
+ var theMousePos = pos ( ( IInputElement ) target ) ;
26
+ return posBounds . Contains ( theMousePos ) ;
27
+ }
28
+ catch ( Exception )
29
+ {
30
+ return false ;
31
+ }
32
+ }
33
+
34
+ private DataGridRow GetDataGridRowItem ( int index )
35
+ {
36
+ if ( RegisteredUserDataGrid . ItemContainerGenerator . Status != System . Windows . Controls . Primitives . GeneratorStatus . ContainersGenerated )
37
+ return null ;
38
+
39
+ return RegisteredUserDataGrid . ItemContainerGenerator . ContainerFromIndex ( index ) as DataGridRow ;
40
+ }
41
+
42
+ private int GetDataGridItemCurrentRowIndex ( GetDragDropPosition pos )
43
+ {
44
+ int curIndex = - 1 ;
45
+ for ( int i = 0 ; i < RegisteredUserDataGrid . Items . Count ; i ++ )
46
+ {
47
+ var item = GetDataGridRowItem ( i ) ;
48
+ if ( IsTheMouseOnTargetRow ( item , pos ) )
49
+ {
50
+ curIndex = i ;
51
+ break ;
52
+ }
53
+ }
54
+ return curIndex ;
55
+ }
56
+ delegate Point GetDragDropPosition ( IInputElement pos ) ;
57
+ private int _prevRowIndex = - 1 ;
58
+ bool _isClick = false ;
59
+ private void DataGrid_Drop ( object sender , DragEventArgs e )
60
+ {
61
+ if ( _prevRowIndex < 0 )
62
+ return ;
63
+
64
+ int index = this . GetDataGridItemCurrentRowIndex ( e . GetPosition ) ;
65
+
66
+ if ( index < 0 )
67
+ return ;
68
+
69
+ if ( index == _prevRowIndex )
70
+ return ;
71
+
72
+ var item = RegisteredUserDataGrid . ItemContainerGenerator . Items [ _prevRowIndex ] ;
73
+
74
+ if ( RegisteredUserDataGrid . ItemsSource is IList collection )
75
+ {
76
+ //ItemsSourceのコレクションの要素を移動させる。
77
+ collection . RemoveAt ( _prevRowIndex ) ;
78
+ collection . Insert ( index , item ) ;
79
+ }
80
+ }
81
+
82
+ private void DataGrid_PreviewMouseLeftButtonDown ( object sender , MouseButtonEventArgs e )
83
+ {
84
+ _prevRowIndex = GetDataGridItemCurrentRowIndex ( e . GetPosition ) ;
85
+
86
+ if ( _prevRowIndex < 0 )
87
+ return ;
88
+
89
+ RegisteredUserDataGrid . SelectedIndex = _prevRowIndex ;
90
+
91
+ //var selected_positionInfo = PositionsGrid.Items[_prevRowIndex];
92
+
93
+ //if (selected_positionInfo == null)
94
+ // return;
95
+
96
+ _isClick = true ;
97
+ }
98
+
99
+ private void PositionsGrid_MouseMove ( object sender , MouseEventArgs e )
100
+ {
101
+ if ( _isClick )
102
+ {
103
+ var item = RegisteredUserDataGrid . Items [ _prevRowIndex ] ;
104
+
105
+ if ( item == null )
106
+ return ;
107
+
108
+ var dragdropeffects = DragDropEffects . Move ;
109
+
110
+ if ( DragDrop . DoDragDrop ( RegisteredUserDataGrid , item , dragdropeffects ) != DragDropEffects . None )
111
+ {
112
+ RegisteredUserDataGrid . SelectedItem = item ;
113
+ _isClick = false ;
114
+ }
115
+ }
116
+ }
117
+
118
+ private void PositionsGrid_MouseLeftButtonUp ( object sender , MouseButtonEventArgs e )
119
+ {
120
+ if ( _isClick )
121
+ {
122
+ _isClick = false ;
123
+ }
124
+ }
26
125
}
27
126
}
0 commit comments