使用DataTrigger启动动画时,动画的已完成事件不会触发?

时间:2014-04-23 16:46:56

标签: c# wpf animation wpf-animation

我的问题:

当我尝试处理DoubleAnimation的Completed时,即使动画是由DataTrigger启动的,也不会触发事件。我正在使用datatrigger开始我的事件,因为它必须从viewmodel启动。我发现Completed事件将触发EventTrigger(如点击按钮)。所以我创建了一个简单的例子来说明双重动画的完成事件在被另一个偶数(按钮点击)触发时触发但不是由数据触发器触发时触发。为什么这和/或我如何解决这种行为?

我的动机(你可以跳过这个)

我正在尝试创建一个具有3种不同状态的控件。当控件进入状态时,我希望它执行动画。我很难让它不止一次重复动画,我知道这是因为动画继续作用于动画的属性,直到它被删除。为了删除动画,我想为已完成的动画事件创建一个事件处理程序。但是,这件事没有解雇。

我的Xaml

<Window x:Class="AnimationTest.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">

<Window.Resources>
    <DataTemplate x:Key="Control">

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding State}" Value="On">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
                                Storyboard.TargetName="txtMessage"
                                Storyboard.TargetProperty="Opacity"
                                From="1.0" To="0.0" Duration="0:0:5"
                                Completed="DataTriggerAnimation_Completed" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger>
        </DataTemplate.Triggers>

        <DockPanel HorizontalAlignment="Center">
            <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
                <Button Content="Start Event Animation" x:Name="Button1" >
                    <Button.Background>
                        <SolidColorBrush x:Name="Button1BackgroundBrush" Color="Red" />
                    </Button.Background>
                    <Button.Triggers>
                        <EventTrigger RoutedEvent="Button.Click">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                Storyboard.TargetName="txtMessage"
                                Storyboard.TargetProperty="Opacity"
                                From="1.0" To="0.0" Duration="0:0:5"
                                FillBehavior="HoldEnd"
                                Completed="EventTriggerAnimation_Completed" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Button.Triggers>
                </Button>

                <Button Content="Data Animation" Click="Button_Click" />
            </StackPanel>

            <TextBlock x:Name="txtMessage" Text="{Binding Message}" />
        </DockPanel>
    </DataTemplate>
</Window.Resources>

<ContentPresenter ContentTemplate="{StaticResource Control}" Content="{Binding}" />

</Window>

背后的代码

public partial class MainWindow : Window
{
    private MainWindowViewModel vm;

    public MainWindow()
    {
        InitializeComponent();

        vm = new MainWindowViewModel();
        this.DataContext = vm;
    }

    private void DataTriggerAnimation_Completed(object sender, EventArgs e)
    {
        Console.WriteLine("Data Trigger Completed");
    }

    private void EventTriggerAnimation_Completed(object sender, EventArgs e)
    {
        Console.WriteLine("Event Trigger Completed");
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        vm.State = MainWindowViewModel.States.On;
    }
}

我的ViewModel

class MainWindowViewModel : INotifyPropertyChanged
{
    private const string message = "This message is visible";

    public string Message
    {
        get { return message; }
    }

    private States state;

    public States State
    {
        get { return state; }
        set 
        { 
            state = value;
            OnPropertyChanged("State");
        }
    }

    public enum States
    {
        Off,
        On
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }

    #endregion
}

如果您想下载项目,请访问https://dl.dropboxusercontent.com/u/104667143/AnimationTest.zip

0 个答案:

没有答案