51618

Stopping an animation in WPF, dynamic loaded usercontrol

Question:

In my main window of the application:

public MainWindow() { InitializeComponent(); Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => { var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea; var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice; var corner = transform.Transform(new Point(workingArea.Right, workingArea.Bottom)); this.Left = corner.X - this.ActualWidth - 100; this.Top = corner.Y - this.ActualHeight; })); NotificationWindow nw1 = new NotificationWindow(); spNotifiers.Children.Add(nw1);

}

My notifier looks like this:

<UserControl x:Class="NotificationWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="Transparent" MouseEnter="UserControl_MouseEnter"> <Grid RenderTransformOrigin="0,1"> <Border BorderThickness="1" Background="Beige" BorderBrush="Black" CornerRadius="10"> <StackPanel Margin="20"> <TextBlock TextWrapping="Wrap" Margin="5"> <Bold>Besked fra podio</Bold><LineBreak /><LineBreak /> Her skal der være en podio notification! </TextBlock> <CheckBox Content="Check check" Margin="5 5 0 5" /> <Button Content="Klik på mig" HorizontalAlignment="Center" /> </StackPanel> </Border> <!-- Animation --> <Grid.Triggers> <EventTrigger RoutedEvent="FrameworkElement.Loaded"> <BeginStoryboard> <Storyboard x:Name="sbMain"> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)"> <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/> <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> <SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/> <SplineDoubleKeyFrame KeyTime="0:0:4" Value="0"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Grid.Triggers> <Grid.RenderTransform> <ScaleTransform ScaleY="1" /> </Grid.RenderTransform> </Grid>

private void UserControl_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) { sbMain.Stop(); // not working

}

From the notifier I have a mouse_anter event, that should stop the animation/storyboard, but no matter what I try I can't make the animation stop.

Any ideas ?

Answer1:

I have found a solution but it requires to remove the Trigger.

Step1: Put your animation into "UserControl.Resources" and give it the attribute "x:Key=sbMain"

Step2: Add an event to UserControl Loaded

Step3: In code behind change "sbMain.Stop();" to "((Storyboard)FindResource("sbMain")).Stop();", may you have to add some using directives.

Step4: To the LoadEvent add "((Storyboard)FindResource("sbMain")).Begin();"

The result looks something like this:

<UserControl x:Class="NotificationWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="Transparent" MouseEnter="UserControl_MouseEnter" Width="300" Height="200" Loaded="UserControl_Loaded"> <UserControl .Resources> <Storyboard x:Key="sbMain"> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" Storyboard.TargetName="AnimatedGrid"> <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/> <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="AnimatedGrid"> <SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/> <SplineDoubleKeyFrame KeyTime="0:0:4" Value="0"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </UserControl .Resources> <Grid RenderTransformOrigin="0,1" x:Name="AnimatedGrid"> <Border BorderThickness="1" Background="Beige" BorderBrush="Black" CornerRadius="10"> <StackPanel Margin="20"> <TextBlock TextWrapping="Wrap" Margin="5"> <Bold>Besked fra podio</Bold><LineBreak /><LineBreak /> Her skal der være en podio notification! </TextBlock> <CheckBox Content="Check check" Margin="5 5 0 5" /> <Button Content="Klik på mig" HorizontalAlignment="Center" /> </StackPanel> </Border> <Grid.RenderTransform> <ScaleTransform ScaleY="1" /> </Grid.RenderTransform> </Grid>

and in code behind:

private void UserControl_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) { ((Storyboard)FindResource("sbMain")).Stop(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { ((Storyboard)FindResource("sbMain")).Begin(); }

this code worked in my case.

Recommend

  • WPF DataTemplate Binding
  • Clr namespace mapping to default xaml namespaces
  • Styling ListBoxItem's of nested ListBoxes [WPF]
  • How to align text in a WPF menu item?
  • WPF MVVM WinformsHost with OpenGL-Control
  • Visualizing 2D Data in a Table
  • Programmatically setting the record pointer in a C# DataGridView
  • How to show complete column as editable in extjs grid(cell editing)?
  • p:panelGrid inside p:panelGrid, how to remove borders in an outer p:panelGrid?
  • WPF auto resize elements
  • jqGrid setting zIndex for alertmod
  • Stalling at deallocate
  • UWP - Show textbox text highlight while textbox is out of focus
  • Apply PivotItemHeader style to PivotItem in UWP
  • Give wx.StaticBitmap a transparent background? wxpython
  • Detaching entity along with referenced entities
  • Can you use multiple instances of Edge on a Selenium Grid Node?
  • Moving Google Chart column annotation position
  • Having an issue with my TextBox control template
  • Powerpoint 2010 VSTO AddIn taskpane on multiple Windows
  • How to clip image to ellipse in XAML
  • removing the default blue color on focus
  • How to delete a newline using \\b
  • Keep transition effect on 1 div from moving the div that follows?
  • How to start server for Selenium grid Java Maven setup
  • Updating one element of a bound Observable collection
  • Allocating a 2D contiguous array within a function
  • How to convert WPF project so it can be used as a class library by a separate exe
  • Android Lock Screen C# .NET Replica
  • Dynamically load css stylesheet and wait for it to load
  • WP8.1 AppBarButton holding event
  • AndEngine Applying Transparancy to AndEngine View
  • How can I speed up CURL tasks?
  • Google Custom Search with transparent background
  • Android fill_parent issue
  • Repeat a vertical line on every page in Report Builder / SSRS
  • CSS Linear-gradient formatting issue accross different browsers
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize