2010-07-01 4 views
0

Je développe un contrôle de numéro de document sur mon application et j'ai écrit un comportement attaché à la zone de texte pour vérifier le texte. Voici le code de comportement:WPF - Styles avec propriétés personnalisées

public class CPFTextBehavior : Behavior<TextBox> 
    { 
     static readonly DependencyPropertyKey IsCPFPropertyKey = 
      DependencyProperty.RegisterAttachedReadOnly("IsCPF", typeof(bool), typeof(CPFTextBehavior), 
       new FrameworkPropertyMetadata(false)); 

     public static readonly DependencyProperty IsCPFProperty = IsCPFPropertyKey.DependencyProperty; 

     public static bool GetIsCPF(TextBox tb) 
     { 
      return (bool)tb.GetValue(IsCPFProperty); 
     } 

     public bool IsCPF 
     { 
      get { return GetIsCPF(AssociatedObject); } 
      private set { AssociatedObject.SetValue(IsCPFPropertyKey, value); } 
     } 

     protected override void OnAttached() 
     { 
      base.OnAttached(); 
      AssociatedObject.TextChanged += LocusProject.Validacao.CPF_CNPJValidation.ValidateCPF; 
      AssociatedObject.PreviewTextInput += LocusProject.Validacao.CPF_CNPJValidation.ValidateCPFMask; 
      DataObject.AddPastingHandler(AssociatedObject, LocusProject.Validacao.CPF_CNPJValidation.PastingCPFMask); 
      AssociatedObject.PreviewKeyDown += Interactivity.PreventInsertKey; 

     } 

     protected override void OnDetaching() 
     { 
      base.OnDetaching(); 
      AssociatedObject.TextChanged -= LocusProject.Validacao.CPF_CNPJValidation.ValidateCPF; 
      AssociatedObject.PreviewTextInput -= LocusProject.Validacao.CPF_CNPJValidation.ValidateCPFMask; 
      DataObject.RemovePastingHandler(AssociatedObject, LocusProject.Validacao.CPF_CNPJValidation.PastingCPFMask); 
      AssociatedObject.PreviewKeyDown -= Interactivity.PreventInsertKey; 
     } 
    } 

Et voici ce que je fais sur mon ResourceDictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:i="clr-namespace:LocusProject"> 

<Style TargetType="{x:Type TextBox}" x:Key="TextFields"> 
    <Setter Property="BorderBrush" Value="DarkBlue"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Style.Triggers> 
     <EventTrigger RoutedEvent="TextBox.GotFocus"> 
      <EventTrigger.Actions> 
       <BeginStoryboard> 
        <Storyboard> 
         <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"> 
          <SplineColorKeyFrame KeyTime="00:00:00.0000000" Value="White"/> 
          <SplineColorKeyFrame KeyTime="00:00:00.3500000" Value="LightBlue"/> 
         </ColorAnimationUsingKeyFrames> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger.Actions> 
     </EventTrigger> 
     <EventTrigger RoutedEvent="TextBox.LostFocus"> 
      <EventTrigger.Actions> 
       <BeginStoryboard> 
        <Storyboard> 
         <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"> 
          <SplineColorKeyFrame KeyTime="00:00:00.0000000" Value="LightBlue"/> 
          <SplineColorKeyFrame KeyTime="00:00:00.3500000" Value="White"/> 
         </ColorAnimationUsingKeyFrames> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger.Actions> 
     </EventTrigger> 
    </Style.Triggers> 
</Style> 

<Style TargetType="{x:Type TextBox}" x:Key="CPFField" BasedOn="{StaticResource TextFields}"> 
      <Setter Property="i:CPFTextBehavior.IsCPF" Value="True" /> 
</Style> 

Mais voici la chose. Il dit "Exception a été lancée par la cible de l'invocation." et je ne peux pas le faire fonctionner.

Est-ce que je fais quelque chose de mal? Merci d'avance.

+0

Le TargetInvocationException a une propriété InnerException . Veuillez vérifier cela pour l'emplacement de la véritable exception. – Femaref

Répondre

0

Vous essayez de définir la valeur de la propriété IsCPF, mais vous avez enregistré cette propriété en lecture seule.

Vous devez:

  1. Modifier l'enregistrement de la propriété:

    static readonly DependencyProperty IsCPFProperty = 
        DependencyProperty.RegisterAttached("IsCPF", typeof(bool), typeof(CPFTextBehavior), new FrameworkPropertyMetadata(false)); 
    
  2. Ajouter une méthode SetIsCPF:

    public static bool SetIsCPF(TextBox tb, bool value) 
    { 
        tb.SetValue(IsCPFProperty, value); 
    } 
    
+0

Merci. Ça a marché! J'ai encore une question à poser. Ai-je vraiment besoin d'insérer les balises l'intérieur de chaque zone de texte? Cela ne fonctionne tout simplement pas lorsque je mets simplement "IsCPF" à true dans le style. –

+0

C'est la manière standard d'attacher Interaction.Behaviours aux objets. En regardant votre code, je ne vois pas comment vous utilisez IsCPF. Serait-ce un vestige de l'ancienne façon de créer des comportements attachés, avant le SDK Expression Blend? –

+0

Je pourrais être un peu perdu. J'ai commencé à développer cette application il y a un mois, je n'avais pas Expression Blend à ce moment-là. J'ai donc importé System.Windows.Interactivity comme référence dans mon projet (j'utilise Visual C# 2010). Le code que j'utilise dans la fenêtre pour un TextBox "CPFish" est Je pensais que c'était tout ce dont j'avais besoin. Y at-il un moyen de faire cela ou je dois inclure ces balises (Interaction.Behaviors et d'autres choses) dans chaque TextBox? Merci –

Questions connexes