2 using System.Windows.Data;
3 using System.Windows.Input;
5 using SiliconStudio.Presentation.Core;
7 namespace SiliconStudio.Presentation.Controls
15 private bool validating;
20 private static readonly DependencyPropertyKey HasTextPropertyKey = DependencyProperty.RegisterReadOnly(
"HasText", typeof(
bool), typeof(
TextBoxBase),
new PropertyMetadata(
false));
25 public static readonly DependencyProperty GetFocusOnLoadProperty = DependencyProperty.Register(
"GetFocusOnLoad", typeof(
bool), typeof(
TextBoxBase),
new PropertyMetadata(
false));
30 public static readonly DependencyProperty SelectAllOnFocusProperty = DependencyProperty.Register(
"SelectAllOnFocus", typeof(
bool), typeof(
TextBoxBase),
new PropertyMetadata(
false));
35 public static readonly DependencyProperty ValidateWithEnterProperty = DependencyProperty.Register(
"ValidateWithEnter", typeof(
bool), typeof(
TextBoxBase),
new PropertyMetadata(
true));
40 public static readonly DependencyProperty ValidateOnTextChangeProperty = DependencyProperty.Register(
"ValidateOnTextChange", typeof(
bool), typeof(
TextBoxBase),
new PropertyMetadata(
false));
45 public static readonly DependencyProperty ValidateOnLostFocusProperty = DependencyProperty.Register(
"ValidateOnLostFocus", typeof(
bool), typeof(
TextBoxBase),
new PropertyMetadata(
true, OnLostFocusActionChanged));
50 public static readonly DependencyProperty CancelWithEscapeProperty = DependencyProperty.Register(
"CancelWithEscape", typeof(
bool), typeof(
TextBoxBase),
new PropertyMetadata(
true));
55 public static readonly DependencyProperty CancelOnLostFocusProperty = DependencyProperty.Register(
"CancelOnLostFocus", typeof(
bool), typeof(
TextBoxBase),
new PropertyMetadata(
false, OnLostFocusActionChanged));
60 public static readonly DependencyProperty ValidateCommandProperty = DependencyProperty.Register(
"ValidateCommand", typeof(ICommand), typeof(
TextBoxBase));
65 public static readonly DependencyProperty ValidateCommandParameterProprty = DependencyProperty.Register(
"ValidateCommandParameter", typeof(
object), typeof(
TextBoxBase));
70 public static readonly DependencyProperty CancelCommandProperty = DependencyProperty.Register(
"CancelCommand", typeof(ICommand), typeof(
TextBoxBase));
75 public static readonly DependencyProperty CancelCommandParameterProprty = DependencyProperty.Register(
"CancelCommandParameter", typeof(
object), typeof(
TextBoxBase));
80 public static readonly RoutedEvent ValidatingEvent = EventManager.RegisterRoutedEvent(
"Validating", RoutingStrategy.Bubble, typeof(
CancelRoutedEventHandler), typeof(
TextBox));
85 public static readonly RoutedEvent ValidatedEvent = EventManager.RegisterRoutedEvent(
"Validated", RoutingStrategy.Bubble, typeof(ValidationRoutedEventHandler<string>), typeof(
TextBox));
90 public static readonly RoutedEvent CancelledEvent = EventManager.RegisterRoutedEvent(
"Cancelled", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(
TextBox));
94 TextProperty.OverrideMetadata(typeof(
TextBox),
new FrameworkPropertyMetadata(
string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, OnTextChanged, null,
true, UpdateSourceTrigger.Explicit));
105 public bool HasText {
get {
return (
bool)GetValue(HasTextPropertyKey.DependencyProperty); }
private set { SetValue(HasTextPropertyKey, value); } }
110 public bool GetFocusOnLoad {
get {
return (
bool)GetValue(GetFocusOnLoadProperty); } set { SetValue(GetFocusOnLoadProperty, value); } }
115 public bool SelectAllOnFocus {
get {
return (
bool)GetValue(SelectAllOnFocusProperty); } set { SetValue(SelectAllOnFocusProperty, value); } }
120 public bool ValidateWithEnter {
get {
return (
bool)GetValue(ValidateWithEnterProperty); } set { SetValue(ValidateWithEnterProperty, value); } }
125 public bool ValidateOnTextChange {
get {
return (
bool)GetValue(ValidateOnTextChangeProperty); } set { SetValue(ValidateOnTextChangeProperty, value); } }
130 public bool ValidateOnLostFocus {
get {
return (
bool)GetValue(ValidateOnLostFocusProperty); } set { SetValue(ValidateOnLostFocusProperty, value); } }
135 public bool CancelWithEscape {
get {
return (
bool)GetValue(CancelWithEscapeProperty); } set { SetValue(CancelWithEscapeProperty, value); } }
140 public bool CancelOnLostFocus {
get {
return (
bool)GetValue(CancelOnLostFocusProperty); } set { SetValue(CancelOnLostFocusProperty, value); } }
145 public ICommand ValidateCommand {
get {
return (ICommand)GetValue(ValidateCommandProperty); } set { SetValue(ValidateCommandProperty, value); } }
150 public object ValidateCommandParameter {
get {
return GetValue(ValidateCommandParameterProprty); } set { SetValue(ValidateCommandParameterProprty, value); } }
155 public ICommand CancelCommand {
get {
return (ICommand)GetValue(CancelCommandProperty); } set { SetValue(CancelCommandProperty, value); } }
160 public object CancelCommandParameter {
get {
return GetValue(CancelCommandParameterProprty); } set { SetValue(CancelCommandParameterProprty, value); } }
165 public event CancelRoutedEventHandler Validating { add { AddHandler(ValidatingEvent, value); }
remove { RemoveHandler(ValidatingEvent, value); } }
170 public event ValidationRoutedEventHandler<string> Validated { add { AddHandler(ValidatedEvent, value); }
remove { RemoveHandler(ValidatedEvent, value); } }
175 public event RoutedEventHandler Cancelled { add { AddHandler(CancelledEvent, value); }
remove { RemoveHandler(CancelledEvent, value); } }
183 OnValidating(cancelRoutedEventArgs);
184 if (cancelRoutedEventArgs.Cancel)
187 RaiseEvent(cancelRoutedEventArgs);
188 if (cancelRoutedEventArgs.Cancel)
192 var coercedText = CoerceTextForValidation(Text);
193 SetCurrentValue(TextProperty, coercedText);
195 BindingExpression expression = GetBindingExpression(TextProperty);
196 if (expression != null)
197 expression.UpdateSource();
204 RaiseEvent(validatedArgs);
205 if (ValidateCommand != null && ValidateCommand.CanExecute(ValidateCommandParameter))
206 ValidateCommand.Execute(ValidateCommandParameter);
215 BindingExpression expression = GetBindingExpression(TextProperty);
216 if (expression != null)
217 expression.UpdateTarget();
223 RaiseEvent(cancelledArgs);
225 if (CancelCommand != null && CancelCommand.CanExecute(CancelCommandParameter))
226 CancelCommand.Execute(CancelCommandParameter);
267 return MaxLength > 0 && baseValue.Length > MaxLength ? baseValue.Substring(0, MaxLength) : baseValue;
273 base.OnPreviewKeyDown(e);
281 if (e.Key == Key.Enter && ValidateWithEnter)
285 if (e.Key == Key.Escape && CancelWithEscape)
293 base.OnGotKeyboardFocus(e);
295 if (SelectAllOnFocus)
303 if (!IsKeyboardFocusWithin)
305 if (SelectAllOnFocus)
318 if (ValidateOnLostFocus)
322 if (CancelOnLostFocus)
327 base.OnLostKeyboardFocus(e);
330 private void ClearUndoStack()
332 var limit = UndoLimit;
341 Keyboard.Focus(
this);
345 private static void OnTextChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
347 var input = (TextBoxBase)d;
348 input.HasText = e.NewValue != null && ((string)e.NewValue).Length > 0;
349 input.OnTextChanged((string)e.OldValue, (
string)e.NewValue);
350 if (input.ValidateOnTextChange && !input.validating)
354 private static void OnLostFocusActionChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
356 var input = (TextBox)d;
357 if (e.Property == ValidateOnLostFocusProperty && (
bool)e.NewValue)
359 input.SetCurrentValue(CancelOnLostFocusProperty,
false);
361 if (e.Property == CancelOnLostFocusProperty && (
bool)e.NewValue)
363 input.SetCurrentValue(ValidateOnLostFocusProperty,
false);
virtual void OnValidated()
Raised when the current changes have has been validated.
void Validate()
Validates the current changes in the TextBox.
An implementation of the TextBoxBase control that provides additional features such as a proper valid...
override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
virtual string CoerceTextForValidation(string baseValue)
Coerces the text during the validation process. This method is invoked by Validate.
override void OnMouseDown(MouseButtonEventArgs e)
virtual void OnTextChanged(string oldValue, string newValue)
Raised when the text of the TextBox changes.
An implementation of the System.Windows.Controls.TextBox control that provides additional features su...
override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
virtual void OnValidating(CancelRoutedEventArgs e)
Raised when the text of the TextBox is being validated.
virtual void OnCancelled()
Raised when the current changes have been cancelled.
void Cancel()
Cancels the current changes in the TextBox.
delegate void CancelRoutedEventHandler(object sender, CancelRoutedEventArgs e)
override void OnPreviewKeyDown(KeyEventArgs e)