4 using System.Windows.Controls;
5 using System.Windows.Data;
6 using System.Windows.Input;
7 using System.Windows.Interactivity;
9 using SiliconStudio.Presentation.Core;
10 using SiliconStudio.Presentation.Extensions;
12 namespace SiliconStudio.Presentation.Behaviors
28 public static readonly DependencyProperty GetFocusOnLoadProperty = DependencyProperty.Register(
"GetFocusOnLoad", typeof(
bool), typeof(
TextBoxValidationBehavior),
new PropertyMetadata(
false));
33 public static readonly DependencyProperty ValidateWithEnterProperty = DependencyProperty.Register(
"ValidateWithEnter", typeof(
bool), typeof(
TextBoxValidationBehavior),
new PropertyMetadata(
true));
38 public static readonly DependencyProperty ValidateOnLostFocusProperty = DependencyProperty.Register(
"ValidateOnLostFocus", typeof(
bool), typeof(
TextBoxValidationBehavior),
new PropertyMetadata(
true, LostFocusActionChanged));
43 public static readonly DependencyProperty CancelWithEscapeProperty = DependencyProperty.Register(
"CancelWithEscape", typeof(
bool), typeof(
TextBoxValidationBehavior),
new PropertyMetadata(
true));
48 public static readonly DependencyProperty CancelOnLostFocusProperty = DependencyProperty.Register(
"CancelOnLostFocus", typeof(
bool), typeof(
TextBoxValidationBehavior),
new PropertyMetadata(
false, LostFocusActionChanged));
53 public static readonly DependencyProperty ValidateCommandProperty = DependencyProperty.Register(
"ValidateCommand", typeof(ICommand), typeof(
TextBoxValidationBehavior));
58 public static readonly DependencyProperty ValidateCommandParameterProprty = DependencyProperty.Register(
"ValidateCommandParameter", typeof(
object), typeof(
TextBoxValidationBehavior));
63 public static readonly DependencyProperty CancelCommandProperty = DependencyProperty.Register(
"CancelCommand", typeof(ICommand), typeof(
TextBoxValidationBehavior));
68 public static readonly DependencyProperty CancelCommandParameterProprty = DependencyProperty.Register(
"CancelCommandParameter", typeof(
object), typeof(
TextBoxValidationBehavior));
78 public static readonly RoutedEvent ValidatedEvent = EventManager.RegisterRoutedEvent(
"Validated", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(
TextBoxValidationBehavior));
83 public static readonly RoutedEvent CancelledEvent = EventManager.RegisterRoutedEvent(
"Cancelled", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(
TextBoxValidationBehavior));
88 public bool GetFocusOnLoad {
get {
return (
bool)GetValue(GetFocusOnLoadProperty); } set { SetValue(GetFocusOnLoadProperty, value); } }
93 public bool ValidateWithEnter {
get {
return (
bool)GetValue(ValidateWithEnterProperty); } set { SetValue(ValidateWithEnterProperty, value); } }
98 public bool ValidateOnLostFocus {
get {
return (
bool)GetValue(ValidateOnLostFocusProperty); } set { SetValue(ValidateOnLostFocusProperty, value); } }
103 public bool CancelWithEscape {
get {
return (
bool)GetValue(CancelWithEscapeProperty); } set { SetValue(CancelWithEscapeProperty, value); } }
108 public bool CancelOnLostFocus {
get {
return (
bool)GetValue(CancelOnLostFocusProperty); } set { SetValue(CancelOnLostFocusProperty, value); } }
113 public ICommand ValidateCommand {
get {
return (ICommand)GetValue(ValidateCommandProperty); } set { SetValue(ValidateCommandProperty, value); } }
118 public object ValidateCommandParameter {
get {
return GetValue(ValidateCommandParameterProprty); } set { SetValue(ValidateCommandParameterProprty, value); } }
123 public ICommand CancelCommand {
get {
return (ICommand)GetValue(CancelCommandProperty); } set { SetValue(CancelCommandProperty, value); } }
128 public object CancelCommandParameter {
get {
return GetValue(CancelCommandParameterProprty); } set { SetValue(CancelCommandParameterProprty, value); } }
133 public event RoutedEventHandler Validating { add { AssociatedObject.AddHandler(ValidatingEvent, value); }
remove { AssociatedObject.RemoveHandler(ValidatingEvent, value); } }
138 public event RoutedEventHandler Validated { add { AssociatedObject.AddHandler(ValidatedEvent, value); }
remove { AssociatedObject.RemoveHandler(ValidatedEvent, value); } }
143 public event RoutedEventHandler Cancelled { add { AssociatedObject.AddHandler(CancelledEvent, value); }
remove { AssociatedObject.RemoveHandler(CancelledEvent, value); } }
149 AssociatedObject.Loaded += TextBoxLoaded;
150 AssociatedObject.KeyDown += TextBoxKeyDown;
151 AssociatedObject.LostFocus += TextBoxLostFocus;
153 var textBinding = BindingOperations.GetBinding(AssociatedObject, TextBox.TextProperty);
154 if (textBinding != null)
156 if (textBinding.UpdateSourceTrigger != UpdateSourceTrigger.Explicit)
158 var newBinding = textBinding.CloneBinding(textBinding.Mode);
159 AssociatedObject.SetBinding(TextBox.TextProperty, newBinding);
167 AssociatedObject.Loaded -= TextBoxLoaded;
168 AssociatedObject.LostFocus -= TextBoxLostFocus;
169 AssociatedObject.KeyDown -= TextBoxKeyDown;
177 Keyboard.Focus(AssociatedObject);
181 private void TextBoxKeyDown(
object sender, KeyEventArgs e)
183 if (e.Key == Key.Enter && ValidateWithEnter)
187 if (e.Key == Key.Escape && CancelWithEscape)
195 if (ValidateOnLostFocus)
199 if (CancelOnLostFocus)
205 private void ClearUndoStack()
207 var limit = AssociatedObject.UndoLimit;
208 AssociatedObject.UndoLimit = 0;
209 AssociatedObject.UndoLimit = limit;
212 private void Validate()
215 AssociatedObject.RaiseEvent(cancelRoutedEventArgs);
216 if (cancelRoutedEventArgs.Cancel)
221 BindingExpression expression = AssociatedObject.GetBindingExpression(TextBox.TextProperty);
222 if (expression != null)
223 expression.UpdateSource();
228 if (ValidateCommand != null && ValidateCommand.CanExecute(ValidateCommandParameter))
229 ValidateCommand.Execute(ValidateCommandParameter);
234 BindingExpression expression = AssociatedObject.GetBindingExpression(TextBox.TextProperty);
235 if (expression != null)
236 expression.UpdateTarget();
242 if (CancelCommand != null && CancelCommand.CanExecute(CancelCommandParameter))
243 CancelCommand.Execute(CancelCommandParameter);
246 private static void LostFocusActionChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
248 var behavior = (TextBoxValidationBehavior)d;
249 if (e.Property == ValidateOnLostFocusProperty)
251 behavior.SetCurrentValue(CancelOnLostFocusProperty, !(bool)e.NewValue);
253 if (e.Property == CancelOnLostFocusProperty)
255 behavior.SetCurrentValue(ValidateOnLostFocusProperty, !(bool)e.NewValue);
override void OnDetaching()
override void OnAttached()
The dialog has been closed by a cancellation from the user.
This behavior allows to update the Binding of the TextBox.Text property both when the control losts f...
delegate void CancelRoutedEventHandler(object sender, CancelRoutedEventArgs e)