views:

2178

answers:

2
A: 

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.

Jesus Rodriguez
I know both so it's easy for me to conver it. Thank you, I will try it soon enough.
David Brunelle
+4  A: 

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" />
John Myczek
I'll try that. I imagine that I can virtually add anything like that. For example, the maximum length of the text inside, which is also another problem I had.
David Brunelle
Forgot to mention, it's the max length of a floating number (the maximum number of decimal and max number of the integer part )
David Brunelle
Yes, attached properties are very powerful and allowed you to add all types of behaviors.
John Myczek