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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
using System;
using System.Collections;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
namespace Xamarin.PropertyEditing.Windows
{
[TemplatePart (Name ="PART_Clear", Type = typeof (Button))]
[TemplatePart (Name = "PART_CompletePopup", Type = typeof (Popup))]
[TemplatePart (Name = "PART_CompleteList", Type = typeof (ListBox))]
internal class TextBoxEx
: TextBox
{
public static readonly DependencyProperty HintProperty = DependencyProperty.Register (
"Hint", typeof(object), typeof(TextBoxEx), new PropertyMetadata (default(object)));
public object Hint
{
get { return (object) GetValue (HintProperty); }
set { SetValue (HintProperty, value); }
}
public static readonly DependencyProperty HintTemplateProperty = DependencyProperty.Register (
"HintTemplate", typeof(DataTemplate), typeof(TextBoxEx), new PropertyMetadata (default(DataTemplate)));
public DataTemplate HintTemplate
{
get { return (DataTemplate) GetValue (HintTemplateProperty); }
set { SetValue (HintTemplateProperty, value); }
}
public static readonly DependencyProperty FocusSelectsAllProperty = DependencyProperty.Register (
"FocusSelectsAll", typeof(bool), typeof(TextBoxEx), new PropertyMetadata (default(bool)));
public bool FocusSelectsAll
{
get { return (bool) GetValue (FocusSelectsAllProperty); }
set { SetValue (FocusSelectsAllProperty, value); }
}
public static readonly DependencyProperty EnableClearProperty = DependencyProperty.Register (
"EnableClear", typeof(bool), typeof(TextBoxEx), new PropertyMetadata (true));
public bool EnableClear
{
get { return (bool) GetValue (EnableClearProperty); }
set { SetValue (EnableClearProperty, value); }
}
public static readonly DependencyProperty ShowClearButtonProperty = DependencyProperty.Register (
"ShowClearButton", typeof(bool), typeof(TextBoxEx), new PropertyMetadata (default(bool)));
public bool ShowClearButton
{
get { return (bool) GetValue (ShowClearButtonProperty); }
set { SetValue (ShowClearButtonProperty, value); }
}
public static readonly DependencyProperty ClearButtonStyleProperty = DependencyProperty.Register (
"ClearButtonStyle", typeof(Style), typeof(TextBoxEx), new PropertyMetadata (default(Style)));
public Style ClearButtonStyle
{
get { return (Style) GetValue (ClearButtonStyleProperty); }
set { SetValue (ClearButtonStyleProperty, value); }
}
public static readonly DependencyProperty EnableSubmitProperty = DependencyProperty.Register (
"EnableSubmit", typeof(bool), typeof(TextBoxEx), new PropertyMetadata (true));
public bool EnableSubmit
{
get { return (bool) GetValue (EnableSubmitProperty); }
set { SetValue (EnableSubmitProperty, value); }
}
public static readonly DependencyProperty SubmitButtonStyleProperty = DependencyProperty.Register (
"SubmitButtonStyle", typeof(Style), typeof(TextBoxEx), new PropertyMetadata (default(Style)));
public Style SubmitButtonStyle
{
get { return (Style) GetValue (SubmitButtonStyleProperty); }
set { SetValue (SubmitButtonStyleProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register (
"ItemsSource", typeof (IEnumerable), typeof (TextBoxEx), new PropertyMetadata (default (IEnumerable)));
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue (ItemsSourceProperty); }
set { SetValue (ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register (
"ItemTemplate", typeof (DataTemplate), typeof (TextBoxEx), new PropertyMetadata (default (DataTemplate)));
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue (ItemTemplateProperty); }
set { SetValue (ItemTemplateProperty, value); }
}
public override void OnApplyTemplate ()
{
base.OnApplyTemplate ();
var clear = GetTemplateChild ("PART_Clear") as Button;
if (clear == null) {
if (!DesignerProperties.GetIsInDesignMode (this))
throw new InvalidOperationException ("PART_Clear must be present as a button");
return;
}
this.popup = (Popup)GetTemplateChild ("PART_CompletePopup");
this.list = (ListBox)GetTemplateChild ("PART_CompleteList");
if (this.popup == null || this.list == null)
throw new InvalidOperationException ("PART_CompletePopup and PART_CompleteList must be present");
this.list.ItemContainerGenerator.ItemsChanged += OnItemsChanged;
this.list.PreviewMouseLeftButtonDown += OnListMouseDown;
clear.Click += (sender, e) => {
Clear();
};
}
protected override void OnPreviewMouseLeftButtonDown (MouseButtonEventArgs e)
{
if (!FocusSelectsAll || IsKeyboardFocusWithin)
return;
e.Handled = true;
Focus ();
SelectAll ();
base.OnPreviewMouseLeftButtonDown (e);
}
protected override void OnGotKeyboardFocus (KeyboardFocusChangedEventArgs e)
{
base.OnGotKeyboardFocus (e);
FocusSelect ();
}
protected override void OnMouseDoubleClick (MouseButtonEventArgs e)
{
base.OnMouseDoubleClick (e);
FocusSelect ();
}
protected override void OnLostKeyboardFocus (KeyboardFocusChangedEventArgs e)
{
if (this.defocusFromList)
return;
base.OnLostKeyboardFocus (e);
this.popup.IsOpen = false;
if (EnableSubmit)
OnSubmit ();
}
protected virtual void OnSubmit()
{
var expression = GetBindingExpression (TextProperty);
expression?.UpdateSource ();
}
protected override void OnPreviewKeyDown (KeyEventArgs e)
{
e.Handled = true;
if (e.Key == Key.Down) {
if (this.list.SelectedIndex == -1 || this.list.SelectedIndex + 1 == this.list.Items.Count)
this.list.SelectedIndex = 0;
else
this.list.SelectedIndex++;
this.list.ScrollIntoView (this.list.SelectedItem);
} else if (e.Key == Key.Up) {
if (this.list.SelectedIndex == -1 || this.list.SelectedIndex == 0)
this.list.SelectedIndex = this.list.Items.Count - 1;
else
this.list.SelectedIndex--;
this.list.ScrollIntoView (this.list.SelectedItem);
} else if (e.Key == Key.Enter || e.Key == Key.Tab) {
if (this.list.SelectedValue != null) {
SelectCompleteItem (this.list.SelectedItem);
} else if (!this.popup.IsOpen) {
if (EnableSubmit)
OnSubmit ();
else
e.Handled = false;
}
} else if (e.Key == Key.Escape) {
if (this.popup.IsOpen)
this.popup.IsOpen = false;
else if (EnableClear)
Clear ();
else
e.Handled = false;
} else {
e.Handled = false;
}
base.OnPreviewKeyDown (e);
}
protected internal bool CanCloseParent ()
{
bool can = !this.defocusFromList;
this.defocusFromList = false;
return can;
}
private bool defocusFromList;
private Popup popup;
private ListBox list;
private void SelectCompleteItem (object item)
{
Text = item.ToString ();
CaretIndex = Text.Length;
this.popup.IsOpen = false;
}
private void FocusSelect()
{
if (!FocusSelectsAll)
return;
if (SelectionLength == 0)
SelectAll ();
}
private void OnItemsChanged (object sender, ItemsChangedEventArgs e)
{
if (!HasEffectiveKeyboardFocus)
return;
this.popup.IsOpen = (this.list.Items.Count > 0);
if (this.list.SelectedIndex == -1)
this.list.SelectedIndex = 0;
}
private void OnListMouseDown (object sender, MouseButtonEventArgs e)
{
Point pos = e.GetPosition (this.list);
var element = this.list.InputHitTest (pos) as FrameworkElement;
var item = element?.FindParentOrSelf<ListBoxItem> ();
if (item == null)
return;
SelectCompleteItem (item.DataContext);
this.defocusFromList = true;
e.Handled = true;
}
}
}
|