开发者

How to trigger animation from one usercontrol from another?

I have an application where there is a MainWindow at the top level with multiple UserC开发者_Go百科ontrols housed inside it. I have a Button on one UserControls and want to trigger an animation in another UserControls. How to go about it? I have tried with Blend but the timeline does not allow me to access the other UserControls.

In short, I want to display a UserControl (say X) beside my existing application that will fade in on a button click. The button click is in another user control say Y, and both the UserControl X and the UserControl Y are inside MainWindow. I hope I have made myself clear.


An example:

<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>

Notes:
1 - The TranslateTransform cannot have a name so you need to navigate to it starting from the UserControl using RenderTransform.X
2 - The event that should trigger the animation needs to be a RoutedEvent, here is the code for the one i have:

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));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜