3
3
using System . Windows ;
4
4
using System . Windows . Automation ;
5
5
using System . Windows . Controls ;
6
+ using System . Windows . Input ;
6
7
7
8
namespace FSClient {
8
9
/// <summary>
@@ -54,6 +55,53 @@ public void Init(String title, IEnumerable<FieldValue> values) {
54
55
55
56
}
56
57
}
58
+ private void CreateSortableContextMenu ( ListBox box ) {
59
+ ContextMenu menu = new ContextMenu ( ) ;
60
+ var item = new MenuItem ( ) { Header = "Move Up" } ;
61
+ item . Click += ( s , e ) => SortableContextMenuMove ( box , - 1 , s , e ) ;
62
+ menu . Items . Add ( item ) ;
63
+ item = new MenuItem ( ) { Header = "Move Down" } ;
64
+ item . Click += ( s , e ) => SortableContextMenuMove ( box , 1 , s , e ) ;
65
+ menu . Items . Add ( item ) ;
66
+ box . ContextMenu = menu ;
67
+ box . ContextMenuOpening += ( s , e ) => SortableMenuOpening ( box , menu , s , e ) ;
68
+ box . PreviewMouseDown += box_MouseDown ;
69
+
70
+ }
71
+
72
+ void box_MouseDown ( object sender , System . Windows . Input . MouseButtonEventArgs e ) {
73
+ if ( e . RightButton == MouseButtonState . Pressed )
74
+ e . Handled = true ;
75
+ }
76
+
77
+
78
+
79
+ private void SortableMenuOpening ( ListBox box , ContextMenu menu , object sender , ContextMenuEventArgs e ) {
80
+ menu . DataContext = ( ( FrameworkElement ) e . OriginalSource ) . DataContext ;
81
+ object elem = menu . DataContext ;
82
+ }
83
+
84
+
85
+ private void SortableContextMenuMove ( ListBox box , int move_by , object sender , RoutedEventArgs e ) {
86
+ MenuItem item = ( sender as MenuItem ) ;
87
+ if ( item == null || item . DataContext == null )
88
+ return ;
89
+ object to_move = item . DataContext ;
90
+ for ( int pos = 0 ; pos < box . Items . Count ; pos ++ ) {
91
+ if ( box . Items [ pos ] == to_move ) {
92
+ if ( pos + move_by < 0 || pos + move_by >= box . Items . Count )
93
+ return ;
94
+ bool selected = box . SelectedItems . Contains ( to_move ) ;
95
+ box . Items . Remove ( to_move ) ;
96
+ box . Items . Insert ( pos + move_by , to_move ) ;
97
+ if ( selected )
98
+ box . SelectedItems . Add ( to_move ) ;
99
+ return ;
100
+ }
101
+ }
102
+
103
+ }
104
+
57
105
private UIElement CreateElementValuer ( FieldValue value ) {
58
106
Control ret = null ;
59
107
switch ( value . field . type ) {
@@ -64,19 +112,27 @@ private UIElement CreateElementValuer(FieldValue value) {
64
112
ret = ibox ;
65
113
break ;
66
114
case Field . FIELD_TYPE . MultiItem :
115
+ case Field . FIELD_TYPE . MultiItemSort :
67
116
ListBox listBox = new ListBox ( ) ;
68
117
listBox . SelectionMode = SelectionMode . Multiple ;
118
+ if ( value . field . type == Field . FIELD_TYPE . MultiItemSort )
119
+ CreateSortableContextMenu ( listBox ) ;
69
120
listBox . Height = 100 ;
70
121
listBox . Width = 190 ;
71
- foreach ( Field . FieldOption option in value . field . options ) {
72
- if ( value . field . Validator == null || String . IsNullOrEmpty ( value . field . Validator ( option . value ) ) )
73
- listBox . Items . Add ( option ) ;
74
- }
75
122
String [ ] vals = value . value . Split ( ',' ) ;
76
123
foreach ( String val in vals ) {
77
124
Field . FieldOption opt = Field . FieldOption . GetByValue ( value . field . options , val ) ;
78
- if ( opt != null )
125
+ if ( opt != null ) {
126
+ listBox . Items . Add ( opt ) ;
79
127
listBox . SelectedItems . Add ( opt ) ;
128
+ }
129
+ }
130
+ foreach ( Field . FieldOption option in value . field . options ) {
131
+ if ( value . field . Validator != null && ! String . IsNullOrEmpty ( value . field . Validator ( option . value ) ) )
132
+ continue ;
133
+ if ( listBox . Items . Contains ( option ) )
134
+ continue ;
135
+ listBox . Items . Add ( option ) ;
80
136
}
81
137
ret = listBox ;
82
138
break ;
@@ -153,9 +209,12 @@ private String GetValueFromUI(FieldValue value, UIElement elem) {
153
209
String val = null ;
154
210
switch ( value . field . type ) {
155
211
case Field . FIELD_TYPE . MultiItem :
212
+ case Field . FIELD_TYPE . MultiItemSort :
156
213
ListBox listBox = ( elem as ListBox ) ;
157
214
val = "" ;
158
- foreach ( Object obj in listBox . SelectedItems ) {
215
+ foreach ( Object obj in listBox . Items ) {
216
+ if ( listBox . SelectedItems . Contains ( obj ) == false )
217
+ continue ;
159
218
Field . FieldOption opt2 = obj as Field . FieldOption ;
160
219
if ( opt2 != null ) {
161
220
if ( ! String . IsNullOrEmpty ( val ) )
0 commit comments