2008-10-09 9 views

Répondre

1

N'ont pas fait beaucoup de ceci encore personnellement, mais quelques bons points de départ here. J'imagine que ceux-ci viendront dans le cadre d'une prochaine version, mais pour l'instant, nous devrons probablement utiliser les validateurs ASP.NET comme point de départ.

4

Vous pouvez lancer et capturer des exceptions de validation de données.

Pour gérer ces deux types d'erreurs doivent prendre 3 étapes:

  1. Identifier le gestionnaire d'erreurs, soit dans le contrôle ou plus dans la hiérarchie de visiblité (par exemple, un récipient, dans ce cas, la grille contient la zone de texte)
  2. Définissez NotifyOnValidationError et ValidateOnException sur true. Ce dernier indique au moteur de liaison de créer un événement d'erreur de validation lorsqu'une exception se produit. Le premier indique au moteur de liaison de déclencher l'événement BindingValidationError lorsqu'une erreur de validation se produit.
  3. Créer le gestionnaire d'événements appelé à l'étape 1.

Tiré de here.

Exemple de code:

// page.xaml.cs 

private bool clean = true; 


private void LayoutRoot_BindingValidationError( 
    object sender, ValidationErrorEventArgs e) 
{ 
    if (e.Action == ValidationErrorEventAction.Added) 
    { 
     QuantityOnHand.Background = new SolidColorBrush(Colors.Red); 
     clean = false; 
    } 
    else if (e.Action == ValidationErrorEventAction.Removed) 
    { 
     QuantityOnHand.Background = new SolidColorBrush(Colors.White); 
     clean = true; 
    } 
} 



// page.xaml 

<Grid x:Name="LayoutRoot" Background="White" BindingValidationError="LayoutRoot_BindingValidationError" > 

<TextBox x:Name="QuantityOnHand" 
    Text="{Binding Mode=TwoWay, Path=QuantityOnHand, 
     NotifyOnValidationError=true, ValidatesOnExceptions=true }" 
    VerticalAlignment="Bottom" 
    HorizontalAlignment="Left" 
    Height="30" Width="90"red 
    Grid.Row="4" Grid.Column="1" /> 


// book.cs 

public int QuantityOnHand 
{ 
    get { return quantityOnHand; } 
    set 
    { 
     if (value < 0) 
     { 
     throw new Exception("Quantity on hand cannot be negative!"); 
     } 
     quantityOnHand = value; 
     NotifyPropertyChanged("QuantityOnHand"); 
    }  // end set 
} 
+0

Je me attendais à voir laquelle le contrôle a provoqué l'exception dans les ValidationErrorEventArgs (http://msdn.microsoft .com/fr-fr/library/system.windows.controls.validationerroreventargs.aspx). J'ai donc déplacé l'événement BindingValidationError hors de la grille, et dans les zones de texte qui peuvent provoquer une exception. De cette façon, je peux vérifier quel contrôle causé dans l'exception dans le paramètre de l'expéditeur. – russau

-1

Vous voudrez peut-être regarder PostSharp, il fait attribuer votre modèle de données côté client très simple.

+0

Si vous fournissez un exemple d'utilisation de PostSharp pour faciliter la validation, je supprime la liste déroulante. –

1

Si vous rencontrez des problèmes pour tenter d'implémenter cela, ce n'est pas parce que votre code est cassé, c'est parce que la fonctionnalité est cassée dans le DataGrid. Découvrez l'article de Jesse Liberty here.

+0

Le bon lien: http://jesseliberty.com/2008/10/22/it-aint-you-babe%E2%80%A6-a-not-a-bug-bug-in-datagrid/ –

+0

@Lukas : lien fixe. –

0

Un code source de contrôle de validation très facile est donnée à cet endroit:

http://silverlightvalidator.codeplex.com/SourceControl/changeset/view/20754#

Voici les conditions dans lesquelles il travaille. 1. L'indicateur indiquant des valeurs non valides suppose que la position du contrôle doit être validée. Par conséquent, les contrôles doivent être étroitement compressés dans une ligne et une colonne de manière à indiquer une position adjacente au contrôle. 2. Cela ne fonctionne pas pour la validation sur ChildWindow.

je dû modifier le code pour inclure les conditions de ChildWindow dans la classe validatorbase comme suit:

0
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; 
using Silverlight.Validators.Controls; 

namespace Silverlight.Validators 
{ 
    public enum ValidationType 
    { 
     Validator, 
     OnDemand 
    } 


    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 bool IsRequired { get; set; } 
     public bool IsValid { get; set; } 
     public Brush InvalidBackground { get; set; } 
     public Brush InvalidBorder { get; set; } 
     public Thickness InvalidBorderThickness { get; set; } 
     public string ErrorMessage { get; set; } 

     private Brush OrigBackground = null; 
     private Brush OrigBorder = null; 
     private Thickness OrigBorderThickness = new Thickness(1); 
     private object OrigTooltip = null; 

     public ValidatorBase() 
     { 
      IsRequired = false; 
      IsValid = true; 
      ManagerName = ""; 
      this.ValidationType = ValidationType.Validator; 
     } 

     public void Initialize(FrameworkElement element) 
     { 
      ElementToValidate = element; 
      element.Loaded += new RoutedEventHandler(element_Loaded); 
     } 

     private bool loaded = false; 
     public UserControl UserControl { get; set; } 
     public ChildWindow ChildUserControl { get; set; } 
     private void element_Loaded(object sender, RoutedEventArgs e) 
     { 
      if (!loaded) 
      { 

       this.UserControl = FindUserControl(ElementToValidate); 
       //UserControl o = FindUserControl(ElementToValidate); 
       this.ChildUserControl = FindChildUserControl(ElementToValidate); 
       //MessageBox.Show(o.GetType().BaseType.ToString()); 
       //no usercontrol. throw error? 
       if ((this.UserControl == null) && (this.ChildUserControl==null)) return; 

       if (this.UserControl != null) 
        this.Manager = FindManager(this.UserControl, ManagerName); 
       else if (this.ChildUserControl != null) 
        this.Manager = FindManager(this.ChildUserControl, ManagerName); 


       if (this.Manager == null) 
       { 
        System.Diagnostics.Debug.WriteLine(String.Format("No ValidatorManager found named '{0}'", ManagerName)); 
        throw new Exception(String.Format("No ValidatorManager found named '{0}'", ManagerName)); 
       } 

       this.Manager.Register(ElementToValidate, this); 

       if (ValidationType == ValidationType.Validator) 
       { 
        ActivateValidationRoutine(); 
       } 

       //Use the properties from the manager if they are not set at the control level 
       if (this.InvalidBackground == null) 
       { 
        this.InvalidBackground = this.Manager.InvalidBackground; 
       } 

       if (this.InvalidBorder == null) 
       { 
        this.InvalidBorder = this.Manager.InvalidBorder; 

        if (InvalidBorderThickness.Bottom == 0) 
        { 
         this.InvalidBorderThickness = this.Manager.InvalidBorderThickness; 
        } 
       } 

       if (this.Indicator ==null) 
       { 
        Type x = this.Manager.Indicator.GetType(); 
        this.Indicator = x.GetConstructor(System.Type.EmptyTypes).Invoke(null) as IIndicator; 
        foreach (var param in x.GetProperties()) 
        { 
         var val = param.GetValue(this.Manager.Indicator, null); 
         if (param.CanWrite && val!= null && val.GetType().IsPrimitive) 
         { 
          param.SetValue(this.Indicator, val, null); 
         } 
        } 
       } 
       loaded = true; 
      } 
      ElementToValidate.Loaded -= new RoutedEventHandler(element_Loaded); 
     } 

     public void SetManagerAndControl(ValidatorManager manager, FrameworkElement element) 
     { 
      this.Manager = manager; 
      this.ElementToValidate = element; 
     } 

     public bool Validate(bool checkControl) 
     { 
      bool newIsValid; 
      if (checkControl) 
      { 
       newIsValid= ValidateControl() && ValidateRequired(); 
      } 
      else 
      { 
       newIsValid = ValidateRequired(); 
      } 

      if (newIsValid && !IsValid) 
      { 
       ControlValid(); 
      } 
      if (!newIsValid && IsValid) 
      { 
       ControlNotValid(); 
      } 
      IsValid=newIsValid; 
      return IsValid; 
     } 

     public virtual void ActivateValidationRoutine() 
     { 
      ElementToValidate.LostFocus += new RoutedEventHandler(ElementToValidate_LostFocus); 
      ElementToValidate.KeyUp += new KeyEventHandler(ElementToValidate_KeyUp); 
     } 

     /// <summary> 
     /// Find the nearest UserControl up the control tree for the FrameworkElement passed in 
     /// </summary> 
     /// <param name="element">Control to validate</param> 
     protected static UserControl FindUserControl(FrameworkElement element) 
     { 
      if (element == null) 
      { 
       return null; 
      } 
      if (element.Parent != null) 
      { 
       //MessageBox.Show(element.Parent.GetType().BaseType.ToString()); 
       if (element.Parent is UserControl) 
       { 
        return element.Parent as UserControl; 
       } 
       return FindUserControl(element.Parent as FrameworkElement); 
      } 
      return null; 
     } 
     protected static ChildWindow FindChildUserControl(FrameworkElement element) 
     { 
      if (element == null) 
      { 
       return null; 
      } 
      if (element.Parent != null) 
      { 
       //MessageBox.Show(element.Parent.GetType().BaseType.ToString()); 
       if (element.Parent is ChildWindow) 
       { 
        return element.Parent as ChildWindow; 
       } 
       return FindChildUserControl(element.Parent as FrameworkElement); 
      } 
      return null; 
     } 

     protected virtual void ElementToValidate_KeyUp(object sender, RoutedEventArgs e) 
     { 
      Dispatcher.BeginInvoke(delegate() { Validate(false); }); 
     } 

     protected virtual void ElementToValidate_LostFocus(object sender, RoutedEventArgs e) 
     { 
      Dispatcher.BeginInvoke(delegate() { Validate(true); }); 
     } 

     protected abstract bool ValidateControl(); 

     protected bool ValidateRequired() 
     { 
      if (IsRequired && ElementToValidate is TextBox) 
      { 
       TextBox box = ElementToValidate as TextBox; 
       return !String.IsNullOrEmpty(box.Text); 
      } 
      return true; 
     } 

     protected void ControlNotValid() 
     { 
      GoToInvalidStyle(); 
     } 

     protected void ControlValid() 
     { 
      GoToValidStyle(); 
     } 

     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 (ElementToValidate is TextBox) 
      { 
       TextBox box = ElementToValidate as TextBox; 

       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 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 (ElementToValidate is TextBox) 
      { 
       TextBox box = ElementToValidate as TextBox; 
       if (OrigBackground != null) 
       { 
        box.Background = OrigBackground; 
       } 

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

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

     protected void SetToolTip(FrameworkElement element, object tooltip) 
     { 
      Dispatcher.BeginInvoke(() => 
         ToolTipService.SetToolTip(element, tooltip)); 
     } 

     private ValidatorManager FindManager(UserControl c, string groupName) 
     { 
      string defaultName = "_DefaultValidatorManager"; 
      var mgr = this.UserControl.FindName(ManagerName); 
      if (mgr == null) 
      { 
       mgr = this.UserControl.FindName(defaultName); 
      } 
      if (mgr == null) 
      { 
       mgr = new ValidatorManager() 
       { 
        Name = defaultName 
       }; 
       Panel g = c.FindName("LayoutRoot") as Panel; 
       g.Children.Add(mgr as ValidatorManager); 
      } 
      return mgr as ValidatorManager; 
     } 

     private ValidatorManager FindManager(ChildWindow c, string groupName) 
     { 
      string defaultName = "_DefaultValidatorManager"; 
      var mgr = this.ChildUserControl.FindName(ManagerName); 
      if (mgr == null) 
      { 
       mgr = this.ChildUserControl.FindName(defaultName); 
      } 
      if (mgr == null) 
      { 
       mgr = new ValidatorManager() 
       { 
        Name = defaultName 
       }; 
       Panel g = c.FindName("LayoutRoot") as Panel; 
       g.Children.Add(mgr as ValidatorManager); 
      } 
      return mgr as ValidatorManager; 
     } 

    } 
}