質問

アプリケーションをチェックするドキュメント番号を作成しており、テキストを確認するために添付の動作をテキストボックスに書きました。これが動作コードです:

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

しかし、ここにあります。 「例外は呼び出の目標によって投げられた」と書かれています。そして、私はそれを機能させることができません。

私は何か間違ったことをしていますか?前もって感謝します。

役に立ちましたか?

解決

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);
    }
    
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top