views:

270

answers:

1

I have some XAML textboxes that need to allow nothing but double and some that need to allow nothing but int.

I could use Binding.ValidationRules with all its code behind, triggers, styles as described here, but isn't there a way for these simple validations to use an attribute something like this, just so nothing but that type can be typed in:

PSEUDO-CODE:

<TextBox Text="{Binding NumberOfContracts}" SimpleValidation="{x:Type sys:Integer}"/>
<TextBox Text="{Binding ContractPrice}" SimpleValidation="{x:Type sys:Double}"/>
+1  A: 

Hey,

I have always thought about something as useful as this but now that I've read your question, I got around to writing one.

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace SimpleValidation
{
    public class SimpleValidator : ValidationRule
    {

        #region Validation Attached Property

        public static Type GetValidationType(DependencyObject obj)
        {
            return (Type)obj.GetValue(ValidationTypeProperty);
        }

        public static void SetValidationType(DependencyObject obj, Type value)
        {
            obj.SetValue(ValidationTypeProperty, value);
        }

        // Using a DependencyProperty as the backing store for ValidationType.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValidationTypeProperty =
            DependencyProperty.RegisterAttached("ValidationType", typeof(Type), typeof(ValidationRule), new UIPropertyMetadata(null, OnValidationTypeChanged));

        private static void OnValidationTypeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            var element = obj as FrameworkElement;
            if (element == null) return;

            // When the element has loaded.
            element.Loaded += (s, e) =>
                                  {
                                      // Create a new validator
                                      var validation = new SimpleValidator(args.NewValue as Type);
                                      // Get the binding expression for the textbox property
                                      var binding = BindingOperations.GetBinding(obj, TextBox.TextProperty);
                                      // If not null and doesn't already contain the validator, then add it.
                                      if (binding != null)
                                          if (!binding.ValidationRules.Contains(validation))
                                              binding.ValidationRules.Add(validation);
                                  };
        }

        #endregion


        #region Validation

        public SimpleValidator() { }
        public SimpleValidator(Type validationType)
        {
            ValidationType = validationType;
        }

        public Type ValidationType { get; set; }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            try
            {
                // Try to convert to the type specified
                Convert.ChangeType(value, ValidationType);
                // Accept value
                return new ValidationResult(true, "Valid value");
            }
            catch (Exception)
            {
                // return invalid type error.
                return new ValidationResult(false, "Value is not of type " + ValidationType.FullName);
            }
        }

        #endregion

    }
}

Usage example:

<TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" validator:SimpleValidator.ValidationType="{x:Type system:Double}" />

Make sure UpdateSourceTrigger reflects the type of update you require, usually PropertyChanged is best. I hope you enjoy it as much as I writing it. Full source code here.

Tri Q