Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7048073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:50:52+00:00 2026-05-28T02:50:52+00:00

I’m developing an application in WPF (.NET 4). I have an attached property that

  • 0

I’m developing an application in WPF (.NET 4).

I have an attached property that I created in order to easily determine if a control is in a “Valid” state.

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

    (...)

I would like to play an animation whenever the value is false. The animation could either be defined within the control itself, or in its template. The name of the storyboard will always be constant, i.e.: “InvalidInput”.

Here is my callback:

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

The problem right now is that I’m unable to find the Storyboard defined within either the control or its template. Both userControlInvalidInputStoryboard and templatedInvalidInputStoryboard are null.

I suspect this is due to the fact that the dependency object is cast to a User Control instead of the real type.

Is there any way to retrieve the template and its storyboard ?

Edit: As requested, here is the XAML of the control which uses this Attached Property

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

And here’s how I instantiated the control:

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

Edit #2: As proposed, I created a Visual State Group in my template:

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

Attempting to change the state of the control does not work, the method returns false:

VisualStateManager.GoToState(invalidControl, "Invalid", true);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:50:53+00:00Added an answer on May 28, 2026 at 2:50 am

    If you implement VisualStates you can forget about the name of the animation and just change the state. Check this article.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.