【问题标题】:WPF - Styles with Custom PropertiesWPF - 具有自定义属性的样式
【发布时间】:2010-07-01 16:31:24
【问题描述】:

我正在开发一个文档编号来检查我的应用程序,并且我将附加的行为写入文本框以检查文本。这是行为代码:

 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>

但事情就是这样。它说“调用目标已引发异常”。我无法让它工作。

我做错了吗? 提前致谢。

【问题讨论】:

  • TargetInvocationException 有一个 InnerException 属性。请检查真正异常的位置。

标签: wpf xaml styles attachedbehaviors


【解决方案1】:

您正在尝试设置 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);
    }
    

【讨论】:

  • 谢谢。有效!不过,我还有一个问题。我真的需要在每个文本框内插入标签 吗?当我在样式中简单地将“IsCPF”设置为 true 时,它​​就不起作用了。
  • 这是将 Interaction.Behaviours 附加到对象的标准方式。查看您的代码,我看不出您是如何使用 IsCPF 的。它可能是在 Expression Blend SDK 之前创建附加行为的旧方法的残余?
  • 我可能有点失落。我一个月前开始开发这个应用程序,那时我还没有 Expression Blend。所以我在我的项目中导入了 System.Windows.Interactivity 作为参考(我使用的是 Visual C# 2010)。我在窗口中为“CPFish”文本框使用的代码是 我认为这就是我所需要的。有没有办法做到这一点,或者我必须在每个 TextBox 中包含这些标签(Interaction.Behaviors 和东西)?谢谢
猜你喜欢
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
  • 1970-01-01
  • 2013-12-05
  • 1970-01-01
  • 2018-04-20
  • 2012-03-08
相关资源
最近更新 更多