开发者

WPF - Styles with Custom Properties

I'm developing a document number checking on my application and I wrote an attached behavior to textbox to check the text. Here's the behavior code:

 public class CPFTextBehavior : Behavior<TextBox>
    {
        static readonly DependencyPropertyKey IsCPFPr开发者_运维技巧opertyKey =
            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;
        }
    }

And here's what i'm doing on my 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>

But here's the thing. It says "Exception has been thrown by the target of invocation." and I can't get it to work.

Am I doing something wrong? Thanks in advance.


You are trying to set the value of the IsCPF property, but you have registered that property as read only.

You need to:

  1. Change the registration of the property:

    static readonly DependencyProperty IsCPFProperty =
        DependencyProperty.RegisterAttached("IsCPF", typeof(bool),  typeof(CPFTextBehavior), new FrameworkPropertyMetadata(false));
    
  2. Add a SetIsCPF method:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜