当前位置: 代码迷 >> C# >> WPF,触发器中的动画怎么修改
  详细解决方案

WPF,触发器中的动画怎么修改

热度:506   发布时间:2016-05-05 05:32:22.0
WPF,触发器中的动画如何修改
一个自定义控件:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPF1">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Name="PART_Border" Background="Red" Height="50" Width="50"/>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard TargetName="PART_Border">
                                        <DoubleAnimation Storyboard.TargetProperty="Height" To="200" Duration="0:0:1" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>


public class CustomControl1 : Control
{
    static CustomControl1()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
    }
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        Trigger trigger = this.Template.Triggers[0] as Trigger;
        BeginStoryboard b = trigger.EnterActions[0] as BeginStoryboard;
        Storyboard sto = b.Storyboard;
        sto.Completed += sto_Completed;
    }
    void sto_Completed(object sender, EventArgs e)
    {
        MessageBox.Show("vhgvh");
    }
}


在上面的自定义控件模板中,有一个触发器,当鼠标移到控件上时,会启动一个动画。
在控件的后台,获取到了触发器中的这个动画,然后为其定义一个Completed事件,也就是动画完毕后的事件。但是出现异常:

我查看了下IsFrozen 属性,这是只读属性,请问,这是怎么回事呢?该怎么修改这个动画呢?
------解决思路----------------------
StoryBoard 对象是 Freezable 且在符合特定条件时, WPF 会将其“冻结”,这时它变为只读,参考
Freezable 对象概述
  相关解决方案