Click here to Skip to main content
15,905,607 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi folks.
I tried to animate my ToggleButton during MouseEnter and MouseLeave.
Below code works when I use directly the color code.
But if I reference it to a resource, it doesn't work and give me exception.

Exeption Thrown:
System.Windows.Markup.XamlParseException: ''Set property 'System.Windows.Media.Animation.ColorAnimation.From' threw an exception.' Line number '137' and line position '46'.'
Inner Exception: ArgumentException: '#FF2E353F' is not a valid value for property 'From'.

Any kindly help please?

What I have tried:

This works Fine.
<BeginStoryboard x:Name="BeginStoryboardNameR">
        <ColorAnimation From="#015CBF" To="#007AFF" 

This code doesn't work and gives me exception.
<SolidColorBrush x:Key="MyBox.TButton.Bg" Color="#FF2E353F"/>

<BeginStoryboard x:Name="BeginStoryboardNameR">
        <ColorAnimation From="{StaticResource MyBox.TButton.Bg}" To="#007AFF"
Updated 13-Dec-23 1:02am
Richard MacCutchan 13-Dec-23 6:38am    
"it doesn't work and give me exception."
You have posted enough questions here to know that we cannot guess what you mean by "it doesn't work", nor can we guess what exceptions you see. So please use the Improve question link above, and add proper details of the problem.
Sh.H. 13-Dec-23 7:02am    
Richard MacCutchan 13-Dec-23 7:13am    
What is it about that exception message that you do not understand?
Sh.H. 17-Dec-23 14:34pm    
I need to use the color as resource, not directly.
Richard MacCutchan 18-Dec-23 3:15am    
Then create a Color resource.

1 solution

Your resource is a SolidColorBrush, not a Color, which is what From expects.

I think your resource should be:
<Color x:Key="MyBox.TButton.Bg">#FF2E353F</Color>
Share this answer
Sh.H. 17-Dec-23 14:34pm    
Doesn't work!
Dave Kreskowiak 17-Dec-23 21:16pm    
Yeah, "doesn't work" is the most useless problem description ever.

Get any error messages? Paste the code as you're using it now, and that includes any other relevant resources and the Button XAML.
Sh.H. 17-Dec-23 23:56pm    
The exception is still the same as before as I wrote in the main topic.
This is the exception:
ArgumentException: '#FF2E353F' is not a valid value for property 'From'.
Sh.H. 17-Dec-23 23:59pm    
I think I should make any change in line Storyboard.TargetProperty
Dave Kreskowiak 18-Dec-23 1:12am    
No, the target property is correct. It's yelling at you because you're not supplying a Color object. In the code you posted above, you're supplying a SolidColorBrush.

You can define a Color and a SolidColorBrush using the same Color, like this:
<Color x:Key="BackgroundColor">LightBlue</Color>
<Color x:Key="HightlightColor">Yellow</Color>
<SolidColorBrush x:Key="BackgroundBrush" Color="{StaticResource BackgroundColor}" />
<SolidColorBrush x:Key="HighlightBrush" Color="{StaticResource HightlightColor}" />

And in your ColorAnimation, you would use it like this:
<ColorAnimation Storyboard.TargetName="Border"
    From="{StaticResource BackgroundColor}" 
    To="{StaticResource HightlightColor}" 
    RepeatBehavior="Forever" />

But, like I said before, you'd have to supply more of the XAML to see what's going on. What element is this StoryBoard inside? Is it in a ControlTemplate? How are you using this? Without this information, it's impossible to tell you what's going on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900