D
D
Dmitry Afonchenko2015-11-10 12:52:04
.NET
Dmitry Afonchenko, 2015-11-10 12:52:04

How to start the animation of the child object on the event of the parent object and vice versa in WPF?

Good afternoon, comrades! Faced such a problem, there is a Grid, inside of which there are different pictures. You need to make sure that when you hover over the Grid, the pictures begin to spin, and when you click on the picture, the Background property of the Grid changes. And in general, the question is, how, on the event of one object, to call the animation of another? How to do this, or at least in which direction to google? Thanks in advance.
This is what is already there, that is, an event that fires when we hover over the image:

<UserControl.Resources>
        <Style TargetType="Image">
            <Setter Property="RenderTransformOrigin" Value="0.5,0.5"></Setter>
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <RotateTransform></RotateTransform>
                </Setter.Value>
            </Setter>   
            
            <Style.Triggers>
                <EventTrigger RoutedEvent="Grid.MouseEnter">
                    <EventTrigger.Actions>
                        <BeginStoryboard Name="rotateStoryBoardBegin">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Angle"
                                                 To="360" Duration="0:0:0.8"
                                                 RepeatBehavior="Forever">
                                </DoubleAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>

                <EventTrigger RoutedEvent="Grid.MouseLeave">
                    <EventTrigger.Actions>
                        <BeginStoryboard Name="rotateStoryBoardEnd">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="RenderTransform.Angle"
                                                 Duration="0:0:0.8">
                                </DoubleAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Afonchenko, 2015-11-10
@Indermove

I found the answer to my question, if you need to access the child element of the grid, when the grid event occurs, then a trigger is added inside the grid and the name of the child element that we want to animate is indicated in the Storyboard.TargetName property. Example:

<Grid.Triggers>
            <EventTrigger RoutedEvent="Grid.MouseEnter">
                <EventTrigger.Actions>
                    <BeginStoryboard Name="rotateStoryBoardBegin">
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="Image" Storyboard.TargetProperty="RenderTransform.Angle"
                                                 To="180" Duration="0:0:0.4">
                            </DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
       <Grid.Triggers>

M
Michael, 2015-11-10
@woberin

You need to dig towards XAML.
An example of rotating an image on hover:

<Grid>
        <Image Height="62" HorizontalAlignment="Left" Margin="259,0,0,176" Stretch="Fill" VerticalAlignment="Bottom" Width="62" Source="Background.jpg" RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <RotateTransform x:Name="Povorot" Angle="0"/>
            </Image.RenderTransform>

            <Image.Triggers>
                <EventTrigger RoutedEvent="Button.MouseEnter">
                    <BeginStoryboard>
                        <BeginStoryboard.Storyboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.25" AutoReverse="False" Storyboard.TargetName="Povorot" Storyboard.TargetProperty="Angle" RepeatBehavior="Forever" From="0" To="200" />
                            </Storyboard>
                        </BeginStoryboard.Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="Button.MouseLeave">
                    <BeginStoryboard>
                        <BeginStoryboard.Storyboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.25" Storyboard.TargetName="Povorot" Storyboard.TargetProperty="Angle" To="0" />
                            </Storyboard>
                        </BeginStoryboard.Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Image.Triggers>
        </Image>
    </Grid>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question