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.Controls;
11 using System.Windows.Data;
12 using System.Windows.Input;
13 using System.Windows.Threading;
15 partial class CaseKeyBox : UserControl, ICaseKeyBoxView
17 public static readonly DependencyProperty DisplayHintTextProperty =
18 DependencyProperty.Register("DisplayHintText", typeof(bool), typeof(CaseKeyBox));
20 public static readonly DependencyProperty LabelTextProperty =
21 DependencyProperty.Register("LabelText", typeof(string), typeof(CaseKeyBox), new UIPropertyMetadata(string.Empty));
23 public static readonly DependencyProperty ValueProperty =
24 DependencyProperty.Register("Value", typeof(object), typeof(CaseKeyBox), new PropertyMetadata(OnValueChanged));
26 public static readonly DependencyProperty ValueTypeProperty =
27 DependencyProperty.Register("ValueType", typeof(Type), typeof(CaseKeyBox), new PropertyMetadata(OnValueTypeChanged));
29 public static RoutedEvent ValueCommittedEvent =
30 EventManager.RegisterRoutedEvent("ValueCommitted", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CaseKeyBox));
32 public static RoutedEvent EditCancelledEvent =
33 EventManager.RegisterRoutedEvent("EditCancelled", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CaseKeyBox));
35 public static readonly DependencyProperty CaseKeyValidationCallbackProperty =
36 DependencyProperty.Register("CaseKeyValidationCallback", typeof(CaseKeyValidationCallbackDelegate), typeof(CaseKeyBox));
38 public static readonly DependencyProperty ErrorCallbackProperty =
39 DependencyProperty.Register("ErrorCallback", typeof(Action<CaseKeyBox>), typeof(CaseKeyBox));
41 public static readonly DependencyProperty CommitExplicitlyProperty =
42 DependencyProperty.Register("CommitExplicitly", typeof(bool), typeof(CaseKeyBox), new PropertyMetadata(false));
49 this.ViewModel = new CaseKeyBoxViewModel(this);
50 InitializeComponent();
53 public event RoutedEventHandler ValueCommitted
55 add { AddHandler(ValueCommittedEvent, value); }
56 remove { RemoveHandler(ValueCommittedEvent, value); }
59 public virtual void OnValueCommitted()
61 RoutedEventArgs args = new RoutedEventArgs();
62 args.RoutedEvent = ValueCommittedEvent;
66 public event RoutedEventHandler EditCancelled
68 add { AddHandler(EditCancelledEvent, value); }
69 remove { RemoveHandler(EditCancelledEvent, value); }
72 public virtual void OnEditCancelled()
74 RoutedEventArgs args = new RoutedEventArgs();
75 args.RoutedEvent = EditCancelledEvent;
79 public CaseKeyValidationCallbackDelegate CaseKeyValidationCallback
81 get { return (CaseKeyValidationCallbackDelegate)GetValue(CaseKeyValidationCallbackProperty); }
82 set { SetValue(CaseKeyValidationCallbackProperty, value); }
85 public Action<CaseKeyBox> ErrorCallback
87 get { return (Action<CaseKeyBox>)GetValue(ErrorCallbackProperty); }
88 set { SetValue(ErrorCallbackProperty, value); }
91 public bool CommitExplicitly
93 get { return (bool)GetValue(CommitExplicitlyProperty); }
94 set { SetValue(CommitExplicitlyProperty, value); }
97 public string LabelText
99 get { return (string)GetValue(LabelTextProperty); }
100 set { SetValue(LabelTextProperty, value); }
103 void DisableKeyboardLostFocus()
105 if (this.visibleBox != null)
107 this.visibleBox.LostKeyboardFocus -= OnLostKeyboardFocus;
111 void EnableKeyboardLostFocus()
113 if (!this.CommitExplicitly)
115 if (this.visibleBox != null)
117 this.visibleBox.LostKeyboardFocus += OnLostKeyboardFocus;
122 void ReportError(string errorMessage)
124 // Invoking error message box will cause LostFocus of the control.
125 // Thus we need to disable LostFocus first and then add the handlers back.
126 DisableKeyboardLostFocus();
127 ErrorReporting.ShowErrorMessage(errorMessage);
129 this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (Action)(() =>
131 if (this.ErrorCallback != null)
133 this.ErrorCallback(this);
134 this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (Action)(() =>
137 EnableKeyboardLostFocus();
143 EnableKeyboardLostFocus();
148 void OnBoxMouseUp(object sender, MouseButtonEventArgs e)
150 // disable the context menu for textbox and combobox
151 if (e.ChangedButton == MouseButton.Right && e.RightButton == MouseButtonState.Released)
157 #region ICaseKeyBoxView Implementation
159 public bool DisplayHintText
161 get { return (bool)GetValue(DisplayHintTextProperty); }
162 set { SetValue(DisplayHintTextProperty, value); }
167 get { return (object)GetValue(ValueProperty); }
168 set { SetValue(ValueProperty, value); }
171 public Type ValueType
173 get { return (Type)GetValue(ValueTypeProperty); }
174 set { SetValue(ValueTypeProperty, value); }
177 public void RegainFocus()
179 if (this.visibleBox != null)
181 Keyboard.Focus((IInputElement)this.visibleBox);
187 #region Delegating Event Handlers
189 public CaseKeyBoxViewModel ViewModel { get; set; }
191 static void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
193 ((CaseKeyBox)sender).ViewModel.OnValueChanged();
196 static void OnValueTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
198 ((CaseKeyBox)sender).ViewModel.OnValueTypeChanged();
201 void OnLabelGotFocus(object sender, RoutedEventArgs e)
203 this.ViewModel.OnLabelGotFocus();
207 void OnLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
211 if (ComboBoxHelper.ShouldFilterUnnecessaryComboBoxEvent(sender as ComboBox))
219 public bool CommitChanges()
221 UpdateSource(this.visibleBox);
222 if (this.CommitExplicitly || this.ViewModel.TextHasBeenChanged())
224 string reason = null;
225 if (!this.ViewModel.CanResolveInputText(out reason))
232 return this.ViewModel.OnLostFocus();
242 public void CancelChanges()
244 DisableKeyboardLostFocus();
245 this.ViewModel.OnEscapePressed(); // simulate cancel
248 void OnBoxLoaded(object sender, RoutedEventArgs e)
250 UIElement box = (UIElement)sender;
251 ComboBox comboBox = box as ComboBox;
252 if (comboBox != null && comboBox.IsVisible)
254 ComboBoxHelper.SynchronizeComboBoxSelection(comboBox, this.ViewModel.Text);
260 Control control = sender as Control;
261 if (control != null && control.Visibility == Visibility.Visible)
263 this.visibleBox = control;
264 EnableKeyboardLostFocus();
267 this.ViewModel.SaveOldText();
270 void OnBoxUnloaded(object sender, RoutedEventArgs e)
272 if (this.visibleBox != null)
274 DisableKeyboardLostFocus();
275 this.visibleBox = null;
279 void OnBoxKeyDown(object sender, KeyEventArgs e)
281 if (!CommitExplicitly)
283 if (e.Key == Key.Escape)
288 else if (e.Key == Key.Enter)
296 void UpdateSource(object sender)
298 if (sender is TextBox)
300 BindingExpression binding = ((TextBox)sender).GetBindingExpression(TextBox.TextProperty);
303 binding.UpdateSource();
306 else if (sender is ComboBox)
308 BindingExpression binding = ((ComboBox)sender).GetBindingExpression(ComboBox.TextProperty);
311 binding.UpdateSource();
318 public void ResetText()
320 this.ViewModel.ResetText();