如何从另一个用户控件触发动画?

时间:2011-04-15 07:42:47

标签: wpf xaml animation user-controls

我有一个应用程序,顶层有一个MainWindow,里面有多个UserControl。我在一个UserControls上有一个Button,想要在另一个UserControls中触发动画。怎么去呢?我尝试使用Blend,但时间轴不允许我访问其他UserControls。

简而言之,我想在我现有的应用程序旁边显示一个UserControl(比如X),它会在点击按钮时淡入。按钮单击位于另一个用户控件中,例如Y,UserControl X和UserControl Y都在MainWindow中。我希望我已经说清楚了。

1 个答案:

答案 0 :(得分:5)

一个例子:

<local:TimeBox x:Name="timeBox">
    <local:TimeBox.RenderTransform>
        <TranslateTransform />
    </local:TimeBox.RenderTransform>
</local:TimeBox>

<local:CustomComboBox>
    <local:CustomComboBox.Triggers>
        <EventTrigger RoutedEvent="local:CustomComboBox.ApplyClick">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.Target="{x:Reference timeBox}"
                                     Storyboard.TargetProperty="RenderTransform.X"
                                     From="-500" To="0" Duration="0:0:1">
                        <DoubleAnimation.EasingFunction>
                            <ExponentialEase Exponent="5" EasingMode="EaseOut"/>
                        </DoubleAnimation.EasingFunction>
                    </DoubleAnimation>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </local:CustomComboBox.Triggers>
</local:CustomComboBox>

备注:
1 - TranslateTransform没有名称,因此您需要使用RenderTransform.X从UserControl开始导航到该名称 2 - 应该触发动画的事件需要是RoutedEvent,这是我所拥有的代码:

public static RoutedEvent ApplyClickEvent = EventManager.RegisterRoutedEvent("ApplyClick",
    RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CustomComboBox));
public event RoutedEventHandler ApplyClick
{
    add { AddHandler(ApplyClickEvent, value); }
    remove { RemoveHandler(ApplyClickEvent, value); }
}

//Pipes the event from an internal button.
private void Button_Apply_Click(object sender, RoutedEventArgs e)
{
    RaiseEvent(new RoutedEventArgs(ApplyClickEvent, this));
}