在WPF中动画UserControl?

时间:2010-05-25 18:37:49

标签: wpf xaml animation user-controls

我有两个xaml文件,一个是 MainWindow.xaml ,另一个是userControl EditTaskView.xaml 。 在MainWindow.xaml中,它由列表框组成,当双击任何列表框项时,它会显示来自EditView userControl的其他窗口(编辑窗口)。每当双击列表框中的任何项目时,我都会尝试为此userControl设置动画。我在userControl中添加了一些动画但是动画只运行一次。如果点击列表框中的任何项目,我怎样才能让我的动画每次运行?

MainWindow.xaml

 <ListBox x:Name="lstBxTask"   Style="{StaticResource ListBoxItems}" MouseDoubleClick="lstBxTask_MouseDoubleClick">
        <ListBox.ItemTemplate>               
            <DataTemplate>                    
                <StackPanel>
                    <Rectangle Style="{StaticResource LineBetweenListBox}"/>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Taskname}"  Style="{StaticResource TextInListBox}"/>
                        <Button Name="btnDelete" Style="{StaticResource DeleteButton}" Click="btnDelete_Click"/>                                                      
                    </StackPanel>
                </StackPanel>                    
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>  
    <ToDoTask:EditTaskView x:Name="EditTask" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Visibility="Collapsed"/>

在MainWindow代码中,有鼠标双击事件,它将EditTaskView的可见性更改为Visible。

建议?

2 个答案:

答案 0 :(得分:0)

您还没有向我们展示您的动画。通常每次触发事件时动画都会>播放:

   <UserControl.Resources>
    <Storyboard x:Key="Storyboard1">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="LayoutRoot">
            <EasingColorKeyFrame KeyTime="0" Value="#FFB62A2A"/>
            <EasingColorKeyFrame KeyTime="0:0:4" Value="#FF2A32B6"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
    <EventTrigger RoutedEvent="Control.MouseDoubleClick">
        <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
    </EventTrigger>
</UserControl.Triggers>

答案 1 :(得分:0)

感谢bitbonk,您的代码确实有帮助。

我想我弄清楚我的问题是什么。我将EventTrigger作为FrameworkElement.Loaded而不是Control.MouseDoubleClick。

无论如何代码看起来像这样:

<Storyboard x:Key="AnimateEditView">
        <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="EditTask">
            <EasingThicknessKeyFrame KeyTime="0" Value="0">
                <EasingThicknessKeyFrame.EasingFunction>
                    <ExponentialEase EasingMode="EaseOut"/>
                </EasingThicknessKeyFrame.EasingFunction>
            </EasingThicknessKeyFrame>
            <EasingThicknessKeyFrame KeyTime="0:0:1.6" Value="0"/>
        </ThicknessAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="EditTask">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1.6" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>

<Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource headerAnimation}"/>
            <BeginStoryboard Storyboard="{StaticResource textBxAnimation}"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="Control.MouseDoubleClick">
            <BeginStoryboard Storyboard="{StaticResource AnimateEditView}"/>
        </EventTrigger>
    </Window.Triggers>