Other controls?

Dec 22, 2008 at 9:46 PM
Any plans to support additional controls like comboboxes, listboxes, etc.?

Thanks-

Jesse Clarke
Coordinator
Dec 23, 2008 at 2:05 PM
It can easily be adapted to do so. Download the source and give it a try.
Dec 31, 2008 at 7:41 PM
Edited Dec 31, 2008 at 7:46 PM
Added support for AutoCompleteBox  - Silverlight Tool Kit,  and SL DatePicker
I'm not sure if I did this as well as some other seasoned coders but here is what I did for my Silverlight - SSRS Reporting Solution:

The current release looks for 4 dependency properties of the TextBox: TextProperty, BorderBrushProperty, BorderThicknessProperty.  My thought was why not get those properties from the element being validated.  So, I added the DependencyProperties to the ValidatorBase Class:

   public abstract class ValidatorBase : DependencyObject
    {
        protected ValidatorManager Manager { get; set; }

        public string ManagerName { get; set; }
        public ValidationType ValidationType { get; set; }
        public IIndicator Indicator { get; set; }
        public FrameworkElement ElementToValidate { get; set; }

        public DependencyProperty BorderBrushProperty { get; set; }
        public DependencyProperty BackgroundProperty { get; set; }
        public DependencyProperty TextProperty { get; set; }
        public DependencyProperty BorderThicknessProperty { get; set; }

        public bool IsRequired { get; set; }
        public bool IsValid { get; set; }

        ...        

Then I added a void to set the properties when the ValidatorBase gets "newed" up, and called it from the Initialize method:

   public void Initialize(FrameworkElement element)
        {
            ElementToValidate = element;
            element.Loaded += new RoutedEventHandler(element_Loaded);
            InitializeValidElements(element);
        }
        //add new controls here:
        public void InitializeValidElements(FrameworkElement ElementToValidate)
        {
            if (ElementToValidate is TextBox)
            {
                this.TextProperty = TextBox.TextProperty;
                this.BorderBrushProperty = TextBox.BorderBrushProperty;
                this.BackgroundProperty = TextBox.BackgroundProperty;
                this.BorderThicknessProperty = TextBox.BorderThicknessProperty;
               
            }
            else if (ElementToValidate is AutoCompleteBox)
            {
                this.TextProperty = AutoCompleteBox.TextProperty;
                this.BorderBrushProperty = AutoCompleteBox.BorderBrushProperty;
                this.BackgroundProperty = AutoCompleteBox.BackgroundProperty;
                this.BorderThicknessProperty = AutoCompleteBox.BorderThicknessProperty;
            }
            else if (ElementToValidate is DatePicker)
            {
                this.TextProperty = DatePicker.TextProperty;
                this.BorderBrushProperty = DatePicker.BorderBrushProperty;
                this.BackgroundProperty = DatePicker.BackgroundProperty;
                this.BorderThicknessProperty = DatePicker.BorderThicknessProperty;
            }
            else
            {
                throw new NotSupportedException();
            }
        }

In the current release, there are 3 methods in the ValidatorBase Class, in which, checks for a TextBox and properties: ValidateRequired(), GoToInvalidStyle(), GoToValidStyle().  I refactored that code like this:

       
          protected bool ValidateRequired()
           {
                   if (IsRequired)
                   {
      
                       return !String.IsNullOrEmpty(ElementToValidate.GetValue(this.TextProperty).ToString());
            }
      
                   return true;
    }

        protected virtual void GoToInvalidStyle()
               {
                   if (!string.IsNullOrEmpty(this.ErrorMessage))
                   {
                       object tooltip = ToolTipService.GetToolTip(ElementToValidate);
      
                       if (tooltip != null)
                       {
                           OrigTooltip = tooltip;
                       }
      
                       //causing a onownermouseleave error currently...
                       this.ElementToValidate.ClearValue(ToolTipService.ToolTipProperty);
      
                       SetToolTip(this.ElementToValidate, this.ErrorMessage);
                   }
      
                   if (Indicator != null)
                   {
                       Indicator.ShowIndicator(this);
                   }
      
                   if (InvalidBackground != null)
                   {
                       if (OrigBackground == null)
                       {
                           OrigBackground = (Brush)ElementToValidate.GetValue(this.BackgroundProperty);
                       }
                       ElementToValidate.SetValue(this.BackgroundProperty, InvalidBackground);
                   }
      
                   if (InvalidBorder != null)
                   {
                       if (OrigBorder == null)
                       {
      
                           OrigBorder = (Brush)ElementToValidate.GetValue(this.BorderBrushProperty);
      
                           OrigBorderThickness = (Thickness)ElementToValidate.GetValue(this.BorderThicknessProperty);
                       }
                       ElementToValidate.SetValue(this.BorderBrushProperty, InvalidBorder);
      
                       if (InvalidBorderThickness != null)
                       {
                           ElementToValidate.SetValue(this.BorderThicknessProperty, InvalidBorderThickness);
                       }
                   }
      
               }
      
               protected virtual void GoToValidStyle()
               {
                   if (!string.IsNullOrEmpty(this.ErrorMessage))
                   {
                       this.ElementToValidate.ClearValue(ToolTipService.ToolTipProperty);
      
                       if (this.OrigTooltip != null)
                       {
                           SetToolTip(this.ElementToValidate, this.OrigTooltip);
                       }
                   }
      
                   if (Indicator != null)
                   {
                       Indicator.HideIndicator();
                   }
      
                   if (OrigBackground != null)
                   {
                       ElementToValidate.SetValue(this.BackgroundProperty, OrigBackground);
      
                   }
      
                   if (OrigBorder != null)
                   {
                       ElementToValidate.SetValue(this.BorderBrushProperty, OrigBorder);
      
                       if (OrigBorderThickness != null)
                       {
                           ElementToValidate.SetValue(this.BorderThicknessProperty, OrigBorderThickness);
                       }
                   }
               }


Lastly, I refactored the overridden ValidateControl() method in the RegexValidator Class.

protected override bool ValidateControl()
         {
             if (RegExpression != null)
             {
                 if (String.IsNullOrEmpty(ElementToValidate.GetValue(this.TextProperty).ToString())) return true;
                 return RegExpression.Match(ElementToValidate.GetValue(this.TextProperty).ToString()).Success;
             }
 
             return true;
         }

Like I said earlier, there is probably a better way to do this. But this way was pretty easy and serves my needs. Hope it helps someone. 

Thanks,
Robert


Apr 9, 2009 at 10:30 AM
Robert,

Thanks for your effort - it worked wonderful for me.
Using your model I've added support for ComboBox control - here are my updates:
on InitializeValidElements and ValidateRequired methods in ValidatorBase class:

public void InitializeValidElements(FrameworkElement ElementToValidate)
        {
            if (ElementToValidate is TextBox)
            {
                this.TextProperty = TextBox.TextProperty;
                this.BorderBrushProperty = TextBox.BorderBrushProperty;
                this.BackgroundProperty = TextBox.BackgroundProperty;
                this.BorderThicknessProperty = TextBox.BorderThicknessProperty;

            }
            else if (ElementToValidate is ComboBox)
            {
                this.TextProperty = ComboBox.SelectedIndexProperty;
                this.BorderBrushProperty = ComboBox.BorderBrushProperty;
                this.BackgroundProperty = ComboBox.BackgroundProperty;
                this.BorderThicknessProperty = ComboBox.BorderThicknessProperty;
            }

           ...
        }

protected bool ValidateRequired()
        {
            if (IsRequired)
            {
                if (ElementToValidate.GetValue(this.TextProperty) != null)
                {
                    switch (ElementToValidate.GetType().Name)
                    {
                        case "ComboBox":
                            return int.Parse(ElementToValidate.GetValue(this.TextProperty).ToString())!=-1;
                            break;
                        default:
                            return !String.IsNullOrEmpty(ElementToValidate.GetValue(this.TextProperty).ToString());
                    }

                }
                else return false;
            }
            return true;
        }

Thank you,
Ionut

Apr 15, 2009 at 7:54 AM
Edited Apr 15, 2009 at 2:45 PM
Added a CompareValidator that also supports PasswordBox.  Not sure if its "professional" as i'm still learning Silverlight, but it worked for my needs.

Did require a change to ValidatorBase.
Went from this:
private Brush OrigBackground = null;
        private Brush OrigBorder = null;
        private Thickness OrigBorderThickness = new Thickness(1);
        private object OrigTooltip = null;
To:
protected Brush OrigBackground = null;
        protected Brush OrigBorder = null;
        protected Thickness OrigBorderThickness = new Thickness(1);
        protected object OrigTooltip = null;


using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight.Validators
{
    public class CompareValidator : ValidatorBase
    {
        public string ElementToCompare { get; set; }
        private FrameworkElement ControlToCompare { get; set; }

        public CompareValidator()
            : base()
        {
        }

        public override void ActivateValidationRoutine()
        {
            if (ControlToCompare == null)
            {
                ControlToCompare = base.UserControl.FindName(ElementToCompare) as FrameworkElement;
throw new ApplicationException("Could not find UserControl");
            }

            base.ActivateValidationRoutine();
            ControlToCompare.LostFocus += new RoutedEventHandler(ElementToValidate_LostFocus);
            ControlToCompare.KeyUp += new KeyEventHandler(ElementToValidate_KeyUp);
        }
        protected override void GoToInvalidStyle()
        {
            GoToInvalidStyle(ElementToValidate);
            GoToInvalidStyle(ControlToCompare);
        }

        private void GoToInvalidStyle(FrameworkElement _element)
        {
            if (!string.IsNullOrEmpty(this.ErrorMessage))
            {
                object tooltip = ToolTipService.GetToolTip(_element);

                if (tooltip != null)
                {
                    OrigTooltip = tooltip;
                }

                //causing a onownermouseleave error currently...
                _element.ClearValue(ToolTipService.ToolTipProperty);

                SetToolTip(_element, this.ErrorMessage);
            }

            if (Indicator != null)
            {
                Indicator.ShowIndicator(this);
            }

            if (_element is TextBox)
            {
                TextBox box = _element as TextBox;
                HandleInvalidTextBox(box);
            }
            else if (_element is PasswordBox)
            {
                PasswordBox pwBox = _element as PasswordBox;
                HandleInvalidTextBox(pwBox);
            }
                
            
        }

        private void HandleInvalidTextBox(TextBox box)
        {
            if (InvalidBackground != null)
            {
                if (OrigBackground == null)
                {
                    OrigBackground = box.Background;
                }
                box.Background = InvalidBackground;
            }

            if (InvalidBorder != null)
            {
                if (OrigBorder == null)
                {
                    OrigBorder = box.BorderBrush;
                    OrigBorderThickness = box.BorderThickness;
                }
                box.BorderBrush = InvalidBorder;

                if (InvalidBorderThickness != null)
                {
                    box.BorderThickness = InvalidBorderThickness;
                }
            }
        }
        private void HandleInvalidTextBox(PasswordBox box)
        {
            if (InvalidBackground != null)
            {
                if (OrigBackground == null)
                {
                    OrigBackground = box.Background;
                }
                box.Background = InvalidBackground;
            }

            if (InvalidBorder != null)
            {
                if (OrigBorder == null)
                {
                    OrigBorder = box.BorderBrush;
                    OrigBorderThickness = box.BorderThickness;
                }
                box.BorderBrush = InvalidBorder;

                if (InvalidBorderThickness != null)
                {
                    box.BorderThickness = InvalidBorderThickness;
                }
            }
        }

        protected override void GoToValidStyle()
        {
            GoToValidStyle(ElementToValidate);
            GoToValidStyle(ControlToCompare);
        }

        private void GoToValidStyle(FrameworkElement _element)
        {
            if (!string.IsNullOrEmpty(this.ErrorMessage))
            {
                _element.ClearValue(ToolTipService.ToolTipProperty);

                if (base.OrigTooltip != null)
                {
                    SetToolTip(_element, this.OrigTooltip);
                }
            }

            if (Indicator != null)
            {
                Indicator.HideIndicator();
            }

            if (_element is TextBox)
            {
                TextBox box = _element as TextBox;
                HandleValidTextBox(box);
            }
            else if (_element is PasswordBox)
            {
                PasswordBox pwBox = _element as PasswordBox;
                HandleValidTextBox(pwBox);
            }
        }

        private void HandleValidTextBox(PasswordBox box)
        {
            if (OrigBackground != null)
            {
                box.Background = OrigBackground;
            }

            if (OrigBorder != null)
            {
                box.BorderBrush = OrigBorder;

                if (OrigBorderThickness != null)
                {
                    box.BorderThickness = OrigBorderThickness;
                }
            }
        }

        private void HandleValidTextBox(TextBox box)
        {
            if (OrigBackground != null)
            {
                box.Background = OrigBackground;
            }

            if (OrigBorder != null)
            {
                box.BorderBrush = OrigBorder;

                if (OrigBorderThickness != null)
                {
                    box.BorderThickness = OrigBorderThickness;
                }
            }
        }

        

        protected override bool ValidateControl()
        {
            if (ControlToCompare is TextBox && ElementToValidate is TextBox)
            {
                return ((TextBox)ControlToCompare).Text == ((TextBox)ElementToValidate).Text;
            }
            else if (ControlToCompare is PasswordBox && ElementToValidate is PasswordBox)
            {
                return ((PasswordBox)ControlToCompare).Password == ((PasswordBox)ElementToValidate).Password;
            }

            return false;
        }
    }
}