获取在附加属性中接收的DependenyObject的模板';s回调

本文关键字:回调 DependenyObject 获取 属性 | 更新日期: 2023-09-27 18:20:04

我正在WPF(.NET4)中开发一个应用程序。

我创建了一个附加属性,以便轻松确定控件是否处于"有效"状态。

    /// <summary>
    /// Initializes static members of the <see cref="ValidationProperty"/> class. 
    /// </summary>
    static ValidationProperty()
    {
        // Register attached dependency property.
        IsValidProperty = DependencyProperty.RegisterAttached("IsValid", typeof(bool),
                                                              typeof(ValidationProperty),
                                                              new FrameworkPropertyMetadata(true, ValidationValueChanged));
    }
    (...)
    /// <summary>
    /// Gets or sets the Dependency Property used to determine if an element is valid.
    /// </summary>
    public static DependencyProperty IsValidProperty { get; set; }
    (...)

只要值为false,我就想播放动画。动画可以在控件本身中定义,也可以在其模板中定义。情节提要的名称将始终保持不变,即:"InvalidInput"。

这是我的回拨:

    /// <summary>
    /// Event raised when the "Is Valid" dependency property's value changes.
    /// </summary>
    /// <param name="sender">The object that raised the event.</param>
    /// <param name="e">The arguments passed to the event.</param>
    private static void ValidationValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var invalidControl = sender as UserControl;
        if (invalidControl == null || !(e.NewValue is bool))
        {
            return;
        }
        var isValid = (bool)e.NewValue;
        if (isValid)
        {
            return;
        }
        var userControlInvalidInputStoryboard = invalidControl.Resources["InvalidInput"] as Storyboard;
        var templatedInvalidInputStoryboard = invalidControl.Template.Resources["InvalidInput"] as Storyboard;
        if (userControlInvalidInputStoryboard != null)
        {
            userControlInvalidInputStoryboard.Begin(invalidControl);
            return;
        }
        if (templatedInvalidInputStoryboard != null)
        {
            templatedInvalidInputStoryboard.Begin(invalidControl, invalidControl.Template);
            return;
        }
    }

现在的问题是,我找不到在控件或其模板中定义的情节提要。userControlInvalidInputStoryboardtemplatedInvalidInputStoreboard均为null。

我怀疑这是由于依赖对象被强制转换为用户控件而不是实际类型。

有什么方法可以检索模板及其故事板吗?

编辑:根据请求,这是使用此附加属性的控件的XAML

<UserControl
   (...)
xmlns:AttachedProperties="clr-namespace:Controls.AttachedProperties"
x:Name="Root">
<Grid x:Name="LayoutRoot">
    <TextBox x:Name="TextBox" 
             Text="{Binding Path=(AttachedProperties:TextProperty.Text), UpdateSourceTrigger=PropertyChanged, 
                            RelativeSource={RelativeSource AncestorType={x:Type Custom:UsernameTextBox}}, Mode=TwoWay,
                            TargetNullValue={x:Static Custom:UsernameTextBox.DefaultKeyword}, FallbackValue=DefaultKeyword}" 
             HorizontalAlignment="Left" Width="292" Style="{DynamicResource StandardTextBoxStyle}" 
             Height="35" VerticalAlignment="Top" TabIndex="0" 
             MaxLines="1" MaxLength="30" 
             FontSize="16" GotFocus="TextBoxGotFocus" LostFocus="TextBoxLostFocus" TextChanged="TextBoxTextChanged" FontWeight="Bold"/>
</Grid>

以下是我如何实例化控件:

<Custom:UsernameTextBox x:Name="UsernameTextbox" 
   AttachedProperties:ValidationProperty.IsValid="{Binding CodexLoginService.UsernameIsValid,   UpdateSourceTrigger=PropertyChanged}" 
   AttachedProperties:TextProperty.Text="{Binding CodexLoginService.Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
   KeyUp="OnUsernameChanged"/>

编辑#2:按照提议,我在我的模板中创建了一个视觉状态组:

        <VisualStateGroup x:Name="InvalidStates">
            <VisualStateGroup.Transitions>
                <VisualTransition GeneratedDuration="0:0:0.2" To="Invalid"/>
            </VisualStateGroup.Transitions>
            <VisualState x:Name="Invalid">
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="InvalidFrame">
                        <EasingDoubleKeyFrame KeyTime="0" Value="1"/>                           </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>

尝试更改控件的状态不起作用,该方法返回false:

VisualStateManager.GoToState(invalidControl, "Invalid", true);

获取在附加属性中接收的DependenyObject的模板';s回调

如果实现VisualStates,您可以忘记动画的名称,只需更改状态。查看这篇文章。