如何从XAML资源(后面的代码中)设置故事板?

时间:2013-09-09 07:48:14

标签: c# wpf xaml storyboard

我是使用WPF的新手,我使用博客文章here创建了一个动画扩展器(似乎是一个受欢迎的第一个控件:)),但我似乎无法从资源中获取故事板。

这是我的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>

背后的相关代码:

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

(这就是整个XAML,如果需要,我可以发布整个c#)

我需要在2 private StoryBoard属性中更改哪些内容才能正确设置它们?

例外情况如下:

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

修改

这在Designer中发生

我点击扩展器: enter image description here

它显示了这个例外: enter image description here

EDIT2

所以我将Storyboard get {}更改为

FindResource("OnCollapsed") as Storyboard;

抛出异常 ResourceReferenceKeyNotFoundException:找不到'OnCollapsed'资源。 .....除了它在XAML中! grr

0 个答案:

没有答案