Validation in textbox in WPF

限于喜欢 提交于 2019-11-29 06:26:26

You can restrict the input to numbers only using an attached property on the TextBox. Define the attached property once (even in a separate dll) and use it on any TextBox. Here is the attached property:

   using System;
   using System.Windows;
   using System.Windows.Controls;
   using System.Windows.Input;

   /// <summary>
   /// Class that provides the TextBox attached property
   /// </summary>
   public static class TextBoxService
   {
      /// <summary>
      /// TextBox Attached Dependency Property
      /// </summary>
      public static readonly DependencyProperty IsNumericOnlyProperty = DependencyProperty.RegisterAttached(
         "IsNumericOnly",
         typeof(bool),
         typeof(TextBoxService),
         new UIPropertyMetadata(false, OnIsNumericOnlyChanged));

      /// <summary>
      /// Gets the IsNumericOnly property.  This dependency property indicates the text box only allows numeric or not.
      /// </summary>
      /// <param name="d"><see cref="DependencyObject"/> to get the property from</param>
      /// <returns>The value of the StatusBarContent property</returns>
      public static bool GetIsNumericOnly(DependencyObject d)
      {
         return (bool)d.GetValue(IsNumericOnlyProperty);
      }

      /// <summary>
      /// Sets the IsNumericOnly property.  This dependency property indicates the text box only allows numeric or not.
      /// </summary>
      /// <param name="d"><see cref="DependencyObject"/> to set the property on</param>
      /// <param name="value">value of the property</param>
      public static void SetIsNumericOnly(DependencyObject d, bool value)
      {
         d.SetValue(IsNumericOnlyProperty, value);
      }

      /// <summary>
      /// Handles changes to the IsNumericOnly property.
      /// </summary>
      /// <param name="d"><see cref="DependencyObject"/> that fired the event</param>
      /// <param name="e">A <see cref="DependencyPropertyChangedEventArgs"/> that contains the event data.</param>
      private static void OnIsNumericOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
         bool isNumericOnly = (bool)e.NewValue;

         TextBox textBox = (TextBox)d;

         if (isNumericOnly)
         {
            textBox.PreviewTextInput += BlockNonDigitCharacters;
            textBox.PreviewKeyDown += ReviewKeyDown;
         }
         else
         {
            textBox.PreviewTextInput -= BlockNonDigitCharacters;
            textBox.PreviewKeyDown -= ReviewKeyDown;
         }
      }

      /// <summary>
      /// Disallows non-digit character.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">An <see cref="TextCompositionEventArgs"/> that contains the event data.</param>
      private static void BlockNonDigitCharacters(object sender, TextCompositionEventArgs e)
      {
         foreach (char ch in e.Text)
         {
            if (!Char.IsDigit(ch))
            {
               e.Handled = true;
            }
         }
      }

      /// <summary>
      /// Disallows a space key.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">An <see cref="KeyEventArgs"/> that contains the event data.</param>
      private static void ReviewKeyDown(object sender, KeyEventArgs e)
      {
         if (e.Key == Key.Space)
         {
            // Disallow the space key, which doesn't raise a PreviewTextInput event.
            e.Handled = true;
         }
      }
   }

Here is how to use it (replace "controls" with your own namespace):

<TextBox controls:TextBoxService.IsNumericOnly="True" />

You can put a validation in your binding

<TextBox>
         <TextBox.Text>
              <Binding Path="CategoriaSeleccionada.ColorFondo"
                       UpdateSourceTrigger="PropertyChanged">
                     <Binding.ValidationRules>
                           <utilities:RGBValidationRule />
                     </Binding.ValidationRules>
               </Binding>
         </TextBox.Text>
</TextBox>

Look at this example (of my program), you put the validation inside the binding like this. With UpdateSourceTrigger you can change when you binding will be updated (lost focus, in every change...)

Well, the validation is a class, I will put you an example:

class RGBValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        // Here you make your validation using the value object.
        // If you want to check if the object is only numbers you can
        // Use some built-in method
        string blah = value.ToString();
        int num;
        bool isNum = int.TryParse(blah, out num);

        if (isNum) return new ValidationResult(true, null);
        else return new ValidationResult(false, "It's no a number");
    }
}

In short, do the job inside that method and return a new ValidationResult. The first parameter is a bool, true if the validation is good, false if not. The second parameter is only a message for information.

I think that this is the basics of textbox validation.

Hope this help.

EDIT: Sorry, I don't know VB.NET but I think that the C# code is pretty simple.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!