I have a StackPanel control, it has an Image located centrally and vertically in the center.

There are 2 MouseMove and MouseLeave events bound to the StackPanel. When I mouse over the Image, the animation is triggered, but when I drag the mouse off the Image, the reverse animation process takes place which is located in MouseLeave (StackPanel). Tobish animation gets lost, although the Image is in the StackPanel. How can I make sure that the MouseLeave method in StackPanel is not called when the cursor is removed from Image? Help plz!

<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication2" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <StackPanel Background="Red" MouseMove="StackPanel_MouseMove" MouseLeave="StackPanel_MouseLeave"> <Image x:Name="Image1" Source="icons8-папка-50.png" Opacity="0.5" Margin="0, 50, 0, 0" HorizontalAlignment="Center" Height="100" /> </StackPanel> <Button Grid.Row="1"/> </Grid> 

 private void StackPanel_MouseMove(object sender, MouseEventArgs e) { DoubleAnimation anim = new DoubleAnimation(Image1.Opacity, 1, TimeSpan.FromSeconds(1)); Image1.BeginAnimation(Image.OpacityProperty, anim); } private void StackPanel_MouseLeave(object sender, MouseEventArgs e) { DoubleAnimation anim = new DoubleAnimation(Image1.Opacity, 0.5, TimeSpan.FromSeconds(1)); Image1.BeginAnimation(Image.OpacityProperty, anim); } 
  • one
    Give the code to reproduce the problem, while it’s not quite clear what is required - Andrey NOP
  • If you quickly move the mouse left / right, then the animation effect seems to be lost from its timing. Try it, you will see that the transparency of the image with the erratically fast movement of the mouse over the StackPanel will become = 1 not in one second of time as indicated in the code. I ask you to petition if I can not clearly explain!) - Victor Manoli
  • If I understand correctly, remove the From from the animation and there will be happiness. In general, my advice to you is to create an animation where it belongs (XAML markup!). - EvgeniyZ
  • Thanks, I will try! - Victor Manoli

0