Error when using StaticResource - silverlight

I'm getting this error
Attribute {StaticResource
StoryboardIntroAnimation} value is out
of range
when I try and use a staic resource as the Storyboard property of a BeginStoryboard object. The markup looks a little like this:
<UserControl ...>
<UserControl.Resources>
<Storyboard x:Key="StoryboardIntroAnimation">
...
</Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger>
<EventTrigger.Actions>
<BeginStoryboard Storyboard="{StaticResource StoryboardIntroAnimation}" />
</EventTrigger.Actions>
</EventTrigger>
</UserControl.Triggers>
...
</UserControl>
Does anyone know why this is happening?

Set RoutedEvent on your EventTrigger ?

Related

Add Color Animation to Mah:Tile

I'm Using MahApps.Metro and I'm using a Tile Element which Is loaded with namespace 'MahCtrl'( xmlns:MahCtrl="http://metro.mahapps.com/winfx/xaml/controls). I want to apply ColorAnimation to the Tile on MouseEnter and MouseLeave.
Here is the xaml snippet I am Currently working on.
<UserControl xmlns:MahCtrl="http://metro.mahapps.com/winfx/xaml/controls">
<MahCtrl:Tile Cursor="Hand" Background="Transparent" Height="200" Width="210"HorizontalContentAlignment="Center">
<MahCtrl:Tile.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Duration="0:0:0.200"
Storyboard.TargetProperty="(MahCtrl:Tile.Background).Color"
To="#fffccc" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Duration="0:0:0.250"
Storyboard.TargetProperty="(MahCtrl:Tile.Background).Color"
To="#ffffff" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</MahCtrl:Tile.Triggers>
</MahCtrl:Tile>
</UserControl>
In the debug section, When I Enter mouse into the Tile the following exception occurs
System.InvalidOperationException: 'Cannot resolve all property
references in the property path '(0).Color'. Verify that applicable
objects support the properties.'
I have tried Using (Background).Color and a lot of other combinations to StoryBoard.TargetProperty But this method works when MahCtrl:Tile element is wrapped with a stack panel and applying event triggers MouseEnter and MouseLeave triggers with target (StackPanel.Background).Color to the StackPanel. How can I target background property of MahCtrl:Tile
It would be a great help if someone can refer to a documentation regarding this topic
Thank you very much,

Storyboard To value staticresource

Can figure this one out, I have some staticresources in aaplication.xaml
These staticresources i use in differtent places, per design. Now i want to use a Staticresource to a Coloranimation in a Storyboard but i can't get it to work i get the error:
An object of the type "System.Windows.Media.SolidColorBrush" cannot be applied to a property that expects the type "System.Nullable1[[System.Windows.Media.Color,....]]
Code so far:
Application.XAML
<Application.Resources>
<SolidColorBrush x:Key="GreenLight" Color="#0CAF12" />
</Application.Resources>
In a usercontrol label style:
<Setter Property="Label.Content" Value="Connected" />
<DataTrigger.EnterActions>
<BeginStoryboard Name="StoryConnected">
<Storyboard Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)">
<ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="{StaticResource GreenLight}" Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="StoryConnected" />
</DataTrigger.ExitActions>
This won't work because the Storyboard cannot be frozen when you bind the To property:
To="{Binding Color, Source={StaticResource GreenLight}}"
So you actually need to set the To property to a Color object, i.e. define your resource like this:
<Color x:Key="GreenLight">#0CAF12</Color>

How to FadeOut Window form on Close Button In WPF C#

int FadeOut;
for (FadeOut = 90; FadeOut >= 10; FadeOut += -10)
{
this.Opacity = FadeOut / 100;
this.refresh();
System.Threading.Thread.Sleep(50);
}
I did something like this but not working
// this.refresh(); is not working
Here's discussed the same problem.
Also you can use triggers: Event triggers or data triggers to initiate animation, and Completed event of animation to set callback on animation end.
Here's data trigger (you should have FadeOut property in your data context)
<Window.Resources>
<Storyboard x:Key="FadeOut">
<DoubleAnimation From="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Duration="0:0:1.5"/>
</Storyboard>
</Window.Resources>
<Window.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=FadeOut}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource FadeOut}"/>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Style>
I did something like this, and you can work on it. Unfortunately you have to implement your own Window title bar :
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Name="myWindow" WindowStyle="None" AllowsTransparency="True"
>
<Window.Resources>
</Window.Resources>
<Grid>
<Button Margin="337,0,0,155">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard >
<Storyboard Duration="0:0:5">
<DoubleAnimation Storyboard.TargetName="myWindow" Storyboard.TargetProperty="Opacity" From="1" To="0"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
</Window>

In WPF animation set property BeginTime to a static resource

What I want to do is define all the BeginTimes of my Animation using a resource.
For example, I want:
<sys:TimeSpan x:key="SomeResource">... </sys:TimeSpan>
...
<DoubleAnimation BeginTime={StaticResource SomeResource}/>
Obviously sys:TimeSpan is not the correct type to use. How do I define my resource so I can reference it as a resource when defining my animations?
I also want to do this purely in XAML.
Thanks.
System.TimeSpan is the correct type to use since is this is the type of BeginTime. You can also do the same for Duration (but using the System.Windows.Duration type instead).
Here is an example using a StaticResource in an animation (after 2 seconds, fade in for 1 second):
<Button Content="Placeholder"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Opacity="0.5">
<Button.Resources>
<sys:TimeSpan x:Key="FadeInBeginTime">0:0:2</sys:TimeSpan>
<Duration x:Key="FadeInDuration">0:0:1</Duration>
</Button.Resources>
<Button.Style>
<Style>
<Style.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseEnter">
<BeginStoryboard x:Name="FadeInBeginStoryBoard">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity"
To="1"
BeginTime="{StaticResource FadeInBeginTime}"
Duration="{StaticResource FadeInDuration}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave">
<StopStoryboard BeginStoryboardName="FadeInBeginStoryBoard" />
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Assuming you have declared the sys namespace as:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Hope this helps!

How do I bind to a color in a WPF ColorAnimation?

I would like to do something that is seemingly quite simple, but I cannot figure out how to do it. I have a ColorAnimation that is triggered when the MouseEnter event occurs. It simply changes the background color of a Border from one color to another color.
Unfortunately, I can't figure out how to put anything but hardcoded colors into this ColorAnimation. So it looks currently like this:
<Style x:Key="MouseOverStyle">
<Style.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="Red" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
However, I'd like do something either like this:
<SolidColorBrush x:Key="MyEventColor" Color="{Binding EventColor}" />
<Style x:Key="MouseOverStyle">
<Style.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="{StaticResource MyEventColor}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
Or like this:
<Style x:Key="MouseOverStyle">
<Style.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="{Binding EventColor}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
When I try to do either of those, an exception gets thrown. For the first, it throws an exception telling me essentially that the "Color" property can't take a SolidColorBrush value...which makes sense...but it certainly doesn't help me out because the ColorAnimation won't let me animate the "(Border.Background).(SolidColorBrush)" property...it will only let me animate the "(Border.Background).(SolidColorBrush.Color)" property....
The exception on the second example basically tells me that it "Cannot freeze this Storyboard timeline tree for use across threads" ...so it sounds like the ColorAnimation is trying to do this binding in some other thread than the UI thread or something? Whatever it's trying to do...it isn't working.
How the heck can I do such a simple task?
For the first one, you could use {StaticResource MyColor} with MyColor defined as such:
<Color x:Key="MyColor">#FF00FF00</Color>
However, this doesn't solve your problem: you can't bind to animation properties since those properties need to be frozen (unchangeable) for the animation to work. Either try to remove your dependence on a binding, or recreate the storyboard with the correct color from code behind when the color changes.

Resources