Monday, 9 September 2013

How do I set a storyboard from the XAML resources (in code behind)?

How do I set a storyboard from the XAML resources (in code behind)?

I am new to using WPF, I have made an animated Expander (seems to be a
popular first control :) ) using the blog post here, but I cannot seem to
get the Storyboard from the Resources...
Here is my XAML:
<Expander xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
x:Class="CustomControls.CustomExpanderControl"
x:Name="ExpanderAnimated"
d:DesignHeight="400" d:DesignWidth="150" Height="24"
Width="150" Opacity="0.5" Header="ExpanderAnimated">
<Expander.Resources>
<Storyboard x:Key="OnExpanded">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="ExpanderAnimated">
<EasingDoubleKeyFrame KeyTime="0" Value="0.5"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(FrameworkElement.Height)"
Storyboard.TargetName="ExpanderAnimated">
<EasingDoubleKeyFrame KeyTime="0" Value="24"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="150"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="OnCollapsed">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="ExpanderAnimated">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.5"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(FrameworkElement.Height)"
Storyboard.TargetName="ExpanderAnimated">
<EasingDoubleKeyFrame KeyTime="0" Value="150"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="24"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Expander.Resources>
<Expander.Triggers>
<EventTrigger RoutedEvent="Expander.Expanded"/>
<EventTrigger RoutedEvent="Expander.Collapsed"/>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource OnExpanded}"/>
<BeginStoryboard Storyboard="{StaticResource OnCollapsed}"/>
</EventTrigger>
</Expander.Triggers>
</Expander>
And the relevant code behind:
private Storyboard sbCollapsed
{
get { return (Storyboard)this.Resources["OnCollapsed"]; }
set { Resources["OnCollapsed"] = value; }
}
private Storyboard sbExpanded
{
get { return (Storyboard)this.Resources["OnExpanded"]; }
set { Resources["OnExpanded"] = value; }
}
private SplineDoubleKeyFrame CollapsedFromHeight
{
get
{
return (SplineDoubleKeyFrame)
((DoubleAnimationUsingKeyFrames)sbCollapsed.Children[0]).KeyFrames[0];
}
}
private SplineDoubleKeyFrame CollapsedToHeight
{
get
{
return (SplineDoubleKeyFrame)
((DoubleAnimationUsingKeyFrames)sbExpanded.Children[0]).KeyFrames[1];
}
}
private SplineDoubleKeyFrame ExpandedFromHeight
{
get
{
return (SplineDoubleKeyFrame)
((DoubleAnimationUsingKeyFrames)sbCollapsed.Children[0]).KeyFrames[0];
}
}
private SplineDoubleKeyFrame ExpandedToHeight
{
get
{
return (SplineDoubleKeyFrame)
((DoubleAnimationUsingKeyFrames)sbExpanded.Children[0]).KeyFrames[1];
}
}
(That is the whole XAML, I can post the whole c# if needed)
What do I need to change in the 2 private StoryBoard properties to set
them correctly?
The exception is as follows:
NullReferenceException: Object reference not set to an instance of an object.
StackTrace
at CustomControls.ExpanderAnimated.get_CollapsedToHeight() // This is the
private property as above
at CustomControls.ExpanderAnimated.get_CollapsedHeight() // This is the
public property
InnerException: None
EDIT
This is happening in the Designer
I click on the Expander:
And it shows this exception:

No comments:

Post a Comment