2010-07-01 123 views
0

我正在开发一个文档编号检查在我的应用程序,我写了一个附加的行为文本框来检查文本。这里的行为代码:WPF - 自定义属性样式

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; 
     } 
    } 

这里是我在做我的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> 

但这里的东西。它说“异常已被调用目标引发”。我无法实现它的工作。

我做错了什么? 在此先感谢。

+0

的TargetInvocationException有一个InnerException属性。请检查真实例外的位置。 – Femaref 2010-07-01 16:33:46

回答

0

您试图设置IsCPF属性的值,但您已将该属性注册为只读。

您需要:

  1. 更改属性的登记:

    static readonly DependencyProperty IsCPFProperty = 
        DependencyProperty.RegisterAttached("IsCPF", typeof(bool), typeof(CPFTextBehavior), new FrameworkPropertyMetadata(false)); 
    
  2. 添加SetIsCPF方法:

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

谢谢。有效!不过,我还有一个问题。我真的需要插入标签 <我:CPFTextBehavior> 每一个文本框里面?只是在样式中将“IsCPF”设置为true时,它不起作用。 – 2010-07-01 16:53:33

+0

这是将Interaction.Behaviours附加到对象的标准方式。看看你的代码,我看不出你是如何使用IsCPF的。在Expression Blend SDK之前,它可能是创建附加行为的旧方式的补充? – 2010-07-01 16:58:04

+0

我可能有点迷路。一个月前我开始开发这个应用程序,那时我没有Expression Blend。所以我在我的项目中引入了System.Windows.Interactivity作为参考(我使用的是Visual C#2010)。我在窗口中用于“CPFish”文本框的代码是 我以为这就是我所需要的。有没有办法做到这一点,或者我必须在每个TextBox中包含这些标签(Interaction.Behaviors和东西)?谢谢 – 2010-07-01 17:04:22