private void EditableTextBoxValidating(object sender, CancelRoutedEventArgs e) { // This may happens somehow when the template is refreshed. if (!ReferenceEquals(sender, editableTextBox)) return; validating = true; UpdateText(); validating = false; var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) e.Cancel = true; }
/// <summary> /// Raised when the text of the TextBox is being validated. /// </summary> /// <param name="e">The event argument.</param> protected virtual void OnValidating(CancelRoutedEventArgs e) { }
private void EditableTextBoxValidating(object sender, CancelRoutedEventArgs e) { // This may happens somehow when the template is refreshed. if (!ReferenceEquals(sender, editableTextBox)) return; // If we require a selected item but there is none, cancel the validation BindingExpression expression; if (RequireSelectedItemToValidate && SelectedItem == null) { e.Cancel = true; expression = GetBindingExpression(TextProperty); expression?.UpdateTarget(); editableTextBox.Cancel(); return; } validating = true; // Update the validated properties ValidatedValue = SelectedValue; ValidatedItem = SelectedItem; // If the dropdown is still open and something is selected, use the string from the selected item if (SelectedItem != null && IsDropDownOpen) { var displayValue = ResolveDisplayMemberValue(SelectedItem); editableTextBox.Text = displayValue?.ToString(); if (editableTextBox.Text != null) editableTextBox.CaretIndex = editableTextBox.Text.Length; } // Update the source of the text property binding expression = GetBindingExpression(TextProperty); expression?.UpdateSource(); // Close the dropdown if (IsDropDownOpen) IsDropDownOpen = false; validating = false; var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) e.Cancel = true; }
/// <summary> /// Validates the current changes in the TextBox. /// </summary> public void Validate() { var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); OnValidating(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) return; RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) return; validating = true; var coercedText = CoerceTextForValidation(Text); SetCurrentValue(TextProperty, coercedText); BindingExpression expression = GetBindingExpression(TextProperty); if (expression != null) expression.UpdateSource(); ClearUndoStack(); var validatedArgs = new ValidationRoutedEventArgs<string>(ValidatedEvent, coercedText); OnValidated(); RaiseEvent(validatedArgs); if (ValidateCommand != null && ValidateCommand.CanExecute(ValidateCommandParameter)) ValidateCommand.Execute(ValidateCommandParameter); validating = false; }
private void EditableTextBoxValidating(object sender, CancelRoutedEventArgs e) { // This may happens somehow when the template is refreshed. if (!ReferenceEquals(sender, editableTextBox)) return; validating = true; // In case we don't need real validation, let's use current textbox value if (!NeedsMatchingItem && listBox.SelectedItem == null) SelectedValue = editableTextBox.Text; UpdateText(); validating = false; var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) e.Cancel = true; }