1
//-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System
.Activities
.Core
.Presentation
8 using System
.Activities
.Presentation
;
10 using System
.Windows
.Automation
;
11 using System
.Windows
.Controls
;
12 using System
.Windows
.Data
;
13 using System
.Windows
.Input
;
14 using System
.Windows
.Threading
;
16 partial class CaseKeyBox
: UserControl
, ICaseKeyBoxView
18 public static readonly DependencyProperty DisplayHintTextProperty
=
19 DependencyProperty
.Register("DisplayHintText", typeof(bool), typeof(CaseKeyBox
));
21 public static readonly DependencyProperty LabelTextProperty
=
22 DependencyProperty
.Register("LabelText", typeof(string), typeof(CaseKeyBox
), new UIPropertyMetadata(string.Empty
));
24 public static readonly DependencyProperty ValueProperty
=
25 DependencyProperty
.Register("Value", typeof(object), typeof(CaseKeyBox
), new PropertyMetadata(OnValueChanged
));
27 public static readonly DependencyProperty ValueTypeProperty
=
28 DependencyProperty
.Register("ValueType", typeof(Type
), typeof(CaseKeyBox
), new PropertyMetadata(OnValueTypeChanged
));
30 public static readonly DependencyProperty EditorAutomationNameProperty
=
31 DependencyProperty
.Register("EditorAutomationName", typeof(string), typeof(CaseKeyBox
));
33 public static readonly DependencyProperty ComboBoxAutomationNameProperty
=
34 DependencyProperty
.Register("ComboBoxAutomationName", typeof(string), typeof(CaseKeyBox
));
36 public static RoutedEvent ValueCommittedEvent
=
37 EventManager
.RegisterRoutedEvent("ValueCommitted", RoutingStrategy
.Bubble
, typeof(RoutedEventHandler
), typeof(CaseKeyBox
));
39 public static RoutedEvent EditCancelledEvent
=
40 EventManager
.RegisterRoutedEvent("EditCancelled", RoutingStrategy
.Bubble
, typeof(RoutedEventHandler
), typeof(CaseKeyBox
));
42 public static readonly DependencyProperty CaseKeyValidationCallbackProperty
=
43 DependencyProperty
.Register("CaseKeyValidationCallback", typeof(CaseKeyValidationCallbackDelegate
), typeof(CaseKeyBox
));
45 public static readonly DependencyProperty ErrorCallbackProperty
=
46 DependencyProperty
.Register("ErrorCallback", typeof(Action
<CaseKeyBox
>), typeof(CaseKeyBox
));
48 public static readonly DependencyProperty CommitExplicitlyProperty
=
49 DependencyProperty
.Register("CommitExplicitly", typeof(bool), typeof(CaseKeyBox
), new PropertyMetadata(false));
56 this.ViewModel
= new CaseKeyBoxViewModel(this);
57 InitializeComponent();
60 public event RoutedEventHandler ValueCommitted
62 add { AddHandler(ValueCommittedEvent, value); }
63 remove { RemoveHandler(ValueCommittedEvent, value); }
66 public virtual void OnValueCommitted()
68 RoutedEventArgs args
= new RoutedEventArgs();
69 args
.RoutedEvent
= ValueCommittedEvent
;
73 public event RoutedEventHandler EditCancelled
75 add { AddHandler(EditCancelledEvent, value); }
76 remove { RemoveHandler(EditCancelledEvent, value); }
79 public virtual void OnEditCancelled()
81 RoutedEventArgs args
= new RoutedEventArgs();
82 args
.RoutedEvent
= EditCancelledEvent
;
86 public CaseKeyValidationCallbackDelegate CaseKeyValidationCallback
88 get { return (CaseKeyValidationCallbackDelegate)GetValue(CaseKeyValidationCallbackProperty); }
89 set { SetValue(CaseKeyValidationCallbackProperty, value); }
92 public Action
<CaseKeyBox
> ErrorCallback
94 get { return (Action<CaseKeyBox>)GetValue(ErrorCallbackProperty); }
95 set { SetValue(ErrorCallbackProperty, value); }
98 public bool CommitExplicitly
100 get { return (bool)GetValue(CommitExplicitlyProperty); }
101 set { SetValue(CommitExplicitlyProperty, value); }
104 public string LabelText
106 get { return (string)GetValue(LabelTextProperty); }
107 set { SetValue(LabelTextProperty, value); }
110 void DisableKeyboardLostFocus()
112 if (this.visibleBox
!= null)
114 this.visibleBox
.LostKeyboardFocus
-= OnLostKeyboardFocus
;
118 void EnableKeyboardLostFocus()
120 if (!this.CommitExplicitly
)
122 if (this.visibleBox
!= null)
124 this.visibleBox
.LostKeyboardFocus
+= OnLostKeyboardFocus
;
129 void ReportError(string errorMessage
)
131 // Invoking error message box will cause LostFocus of the control.
132 // Thus we need to disable LostFocus first and then add the handlers back.
133 DisableKeyboardLostFocus();
134 ErrorReporting
.ShowErrorMessage(errorMessage
);
136 this.Dispatcher
.BeginInvoke(DispatcherPriority
.ApplicationIdle
, (Action
)(() =>
138 if (this.ErrorCallback
!= null)
140 this.ErrorCallback(this);
141 this.Dispatcher
.BeginInvoke(DispatcherPriority
.ApplicationIdle
, (Action
)(() =>
144 EnableKeyboardLostFocus();
150 EnableKeyboardLostFocus();
155 void OnBoxMouseUp(object sender
, MouseButtonEventArgs e
)
157 // disable the context menu for textbox and combobox
158 if (e
.ChangedButton
== MouseButton
.Right
&& e
.RightButton
== MouseButtonState
.Released
)
164 #region ICaseKeyBoxView Implementation
166 public bool DisplayHintText
168 get { return (bool)GetValue(DisplayHintTextProperty); }
169 set { SetValue(DisplayHintTextProperty, value); }
174 get { return (object)GetValue(ValueProperty); }
175 set { SetValue(ValueProperty, value); }
178 public Type ValueType
180 get { return (Type)GetValue(ValueTypeProperty); }
181 set { SetValue(ValueTypeProperty, value); }
184 public string ComboBoxAutomationName
186 get { return (string)GetValue(ComboBoxAutomationNameProperty); }
187 set { SetValue(ComboBoxAutomationNameProperty, value); }
190 public string EditorAutomationName
192 get { return (string)GetValue(EditorAutomationNameProperty); }
193 set { SetValue(EditorAutomationNameProperty, value); }
196 public void RegainFocus()
198 if (this.visibleBox
!= null)
200 Keyboard
.Focus((IInputElement
)this.visibleBox
);
206 #region Delegating Event Handlers
208 public CaseKeyBoxViewModel ViewModel { get; set; }
210 static void OnValueChanged(DependencyObject sender
, DependencyPropertyChangedEventArgs eventArgs
)
212 ((CaseKeyBox
)sender
).ViewModel
.OnValueChanged();
215 static void OnValueTypeChanged(DependencyObject sender
, DependencyPropertyChangedEventArgs eventArgs
)
217 ((CaseKeyBox
)sender
).ViewModel
.OnValueTypeChanged();
220 void OnLabelGotFocus(object sender
, RoutedEventArgs e
)
222 this.ViewModel
.OnLabelGotFocus();
226 void OnLostKeyboardFocus(object sender
, KeyboardFocusChangedEventArgs e
)
230 if (ComboBoxHelper
.ShouldFilterUnnecessaryComboBoxEvent(sender
as ComboBox
))
238 public bool CommitChanges()
240 UpdateSource(this.visibleBox
);
241 if (this.CommitExplicitly
|| this.ViewModel
.TextHasBeenChanged())
243 string reason
= null;
244 if (!this.ViewModel
.CanResolveInputText(out reason
))
251 return this.ViewModel
.OnLostFocus();
261 public void CancelChanges()
263 DisableKeyboardLostFocus();
264 this.ViewModel
.OnEscapePressed(); // simulate cancel
267 void OnBoxLoaded(object sender
, RoutedEventArgs e
)
269 UIElement box
= (UIElement
)sender
;
270 ComboBox comboBox
= box
as ComboBox
;
271 if (comboBox
!= null)
273 if (!LocalAppContextSwitches
.UseLegacyAccessibilityFeatures
)
275 if (!string.IsNullOrEmpty(ComboBoxAutomationName
))
277 comboBox
.SetValue(AutomationProperties
.NameProperty
, ComboBoxAutomationName
);
279 if (comboBox
.IsEditable
&& comboBox
.Template
!= null)
281 var comboBoxTextBox
= comboBox
.Template
.FindName("PART_EditableTextBox", comboBox
) as TextBox
;
282 if (comboBoxTextBox
!= null && !string.IsNullOrEmpty(EditorAutomationName
))
284 comboBoxTextBox
.SetValue(AutomationProperties
.NameProperty
, EditorAutomationName
);
289 if (comboBox
.IsVisible
)
291 ComboBoxHelper
.SynchronizeComboBoxSelection(comboBox
, this.ViewModel
.Text
);
295 if (!LocalAppContextSwitches
.UseLegacyAccessibilityFeatures
)
297 TextBox textBox
= box
as TextBox
;
298 if (textBox
!= null && !string.IsNullOrEmpty(EditorAutomationName
))
300 textBox
.SetValue(AutomationProperties
.NameProperty
, EditorAutomationName
);
308 Control control
= sender
as Control
;
309 if (control
!= null && control
.Visibility
== Visibility
.Visible
)
311 this.visibleBox
= control
;
312 EnableKeyboardLostFocus();
315 this.ViewModel
.SaveOldText();
318 void OnBoxUnloaded(object sender
, RoutedEventArgs e
)
320 if (this.visibleBox
!= null)
322 DisableKeyboardLostFocus();
323 this.visibleBox
= null;
327 void OnBoxKeyDown(object sender
, KeyEventArgs e
)
329 if (!CommitExplicitly
)
331 if (e
.Key
== Key
.Escape
)
336 else if (e
.Key
== Key
.Enter
)
344 void UpdateSource(object sender
)
346 if (sender
is TextBox
)
348 BindingExpression binding
= ((TextBox
)sender
).GetBindingExpression(TextBox
.TextProperty
);
351 binding
.UpdateSource();
354 else if (sender
is ComboBox
)
356 BindingExpression binding
= ((ComboBox
)sender
).GetBindingExpression(ComboBox
.TextProperty
);
359 binding
.UpdateSource();
366 public void ResetText()
368 this.ViewModel
.ResetText();