How to create BeginStoryboard in code behind for WPF?

I have the following XAML and I wish to convert this to code behind, I have been able to successfully create the animations so the control fades in & out as expected, but I'm having trouble converting the IsMouseOver trigger to code behind:

<DataTemplate.Triggers> <EventTrigger RoutedEvent="Control.Loaded" SourceName="NotificationGrid"> <BeginStoryboard x:Name="BeginNotificationStoryboard"> <Storyboard x:Name="NotificationStoryboard"> <DoubleAnimation Storyboard.TargetName="NotificationGrid" From="0.01" To="1" Storyboard.TargetProperty="Opacity" Duration="0:0:0.5" /> <DoubleAnimation Storyboard.TargetName="NotificationGrid" From="1" To="0" Storyboard.TargetProperty="Opacity" Duration="0:0:0.5" BeginTime="0:0:5" /> </Storyboard> </BeginStoryboard> </EventTrigger> <Trigger Property="IsMouseOver" Value="True"> <Trigger.EnterActions> <SeekStoryboard Offset="0:0:3" BeginStoryboardName="BeginNotificationStoryboard" /> <PauseStoryboard BeginStoryboardName="BeginNotificationStoryboard" /> </Trigger.EnterActions> <Trigger.ExitActions> <SeekStoryboard Offset="0:0:3" BeginStoryboardName="BeginNotificationStoryboard" /> <ResumeStoryboard BeginStoryboardName="BeginNotificationStoryboard" /> </Trigger.ExitActions> </Trigger> </DataTemplate.Triggers> </DataTemplate>

The issue I am have is how do I know what the 'BeginStoryboardName' value should be for the instances of the SeekStoryboard classes since I haven't had to create a BeginStoryboard instance to get the animation on loading to work as expected.

var loadingAnimation = new DoubleAnimation(0.01, 1, new Duration(TimeSpan.FromSeconds(0.5))); var closingAnimation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(3))) { BeginTime = TimeSpan.FromSeconds(5) }; Storyboard.SetTarget(loadingAnimation, AssociatedObject); Storyboard.SetTarget(closingAnimation, AssociatedObject); Storyboard.SetTargetProperty(loadingAnimation, new PropertyPath(UIElement.OpacityProperty)); Storyboard.SetTargetProperty(closingAnimation, new PropertyPath(UIElement.OpacityProperty)); Storyboard.SetTarget(loadingAnimation, AssociatedObject); Storyboard.SetTarget(closingAnimation, AssociatedObject); var storyboard = new Storyboard(); storyboard.Children.Add(loadingAnimation); storyboard.Children.Add(closingAnimation); var enterSeekStoryboard = new SeekStoryboard { Offset = TimeSpan.FromSeconds(5), // What value should go here? BeginStoryboardName = "" }; var exitSeekStoryboard = new SeekStoryboard { Offset = TimeSpan.FromSeconds(5), // What value should go here? BeginStoryboardName = "" }; var trigger = new Trigger { Property = UIElement.IsMouseOverProperty, Value = true }; trigger.EnterActions.Add(enterSeekStoryboard); trigger.ExitActions.Add(exitSeekStoryboard); var style = new Style(); style.Triggers.Add(trigger); AssociatedObject.Style = style; storyboard.Completed += HandleOnCompleted; storyboard.Begin();

-------------Problems Reply------------

Your full code would be something like this. I've left some comments to point out some issues I've found:

var loadingAnimation = new DoubleAnimation(0.01, 1, new Duration(TimeSpan.FromSeconds(0.5)));
var closingAnimation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(3)))
{
BeginTime = TimeSpan.FromSeconds(5)
};

Storyboard.SetTarget(loadingAnimation, AssociatedObject);
Storyboard.SetTarget(closingAnimation, AssociatedObject);

Storyboard.SetTargetProperty(loadingAnimation, new PropertyPath(UIElement.OpacityProperty));
Storyboard.SetTargetProperty(closingAnimation, new PropertyPath(UIElement.OpacityProperty));

Storyboard.SetTarget(loadingAnimation, AssociatedObject);
Storyboard.SetTarget(closingAnimation, AssociatedObject);

var storyboard = new Storyboard();
storyboard.Children.Add(loadingAnimation);
storyboard.Children.Add(closingAnimation);
// Subscription to events must be done at this point, because the Storyboard object becomes frozen later on
storyboard.Completed += HandleOnCompleted;

string storyBoardName = "BeginNotificationStoryboard";

// We define the BeginStoryBoard action for the EventTrigger
var beginStoryboard = new BeginStoryBoard();
beginStoryboard.Name = storyBoardName;
beginStoryboard.Storyboard = storyboard;

// We create the EventTrigger
var eventTrigger = new EventTrigger(Control.LoadedEvent);
eventTrigger.Actions.Add(beginStoryboard);

// Actions for the entering animation
var enterSeekStoryboard = new SeekStoryboard
{
Offset = TimeSpan.FromSeconds(5),
BeginStoryboardName = storyBoardName
};
var enterPauseStoryboard = new PauseStoryboard
{
BeginStoryboardName = storyBoardName
};

// Actions for the exiting animation
var exitSeekStoryboard = new SeekStoryboard
{
Offset = TimeSpan.FromSeconds(5),
BeginStoryboardName = storyBoardName
};
var exitResumeStoryboard = new ResumeStoryboard
{
BeginStoryboardName = storyBoardName
};

var trigger = new Trigger
{
Property = UIElement.IsMouseOverProperty,
Value = true
};

trigger.EnterActions.Add(enterSeekStoryboard);
trigger.EnterActions.Add(enterPauseStoryboard);
trigger.ExitActions.Add(exitSeekStoryboard);
trigger.ExitActions.Add(exitResumeStoryboard);

var style = new Style();
// The name of the Storyboard must be registered so the actions can find it
style.RegisterName(storyBoardName, beginStoryboard);
// Add both the EventTrigger and the regular Trigger
style.Triggers.Add(eventTrigger);
style.Triggers.Add(trigger);

AssociatedObject.Style = style;

// No need for storyboard.Begin()

You have created Storyboard but not BeginStoryboard. So do it like this:

var storyboard = new Storyboard();
storyboard.Children.Add(loadingAnimation);
storyboard.Children.Add(closingAnimation);

var beginStoryboard = new BeginStoryboard(){ Name="BeginNotificationStoryboard", Storyboard = storyboard};

var enterSeekStoryboard = new SeekStoryboard
{
Offset = TimeSpan.FromSeconds(5),
// What value should go here?
BeginStoryboardName = "BeginNotificationStoryboard"
};

Dont forget to add your beginStoryboard to EventTrigger like it was before.

Actually original XAML code used BeginNotificationStoryboard id just to remove some code duplicates. If you don't want it anymore just add it to TriggerActions like this:

trigger.EnterActions.Add(beginStoryboard);

Than you don't need to specify the name.

Category:c# Views:255 Time:2019-03-20
Tags: wpf xaml

Related post

  • How to create collection view source in code behind for wpf app 2012-01-12

    I have following code public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var entities = new DemoEntities(); var depts = entities.Depts.ToList(); // entity framwork dept table CollectionViewSource cvs = (Collection

  • How to create folder in code behind page 2009-04-03

    This is in reference to yesterday's question "How do I create folders in ASP.NET in code behind". The problem is that I want to create dynamic folders at run time. Folder names will be entered via a TextBox and output will be displayed in a TreeView.

  • How to dynamically add a code behind for button 2011-11-21

    Button bn = new Button(); bn.Location = new System.Drawing.Point(560, 350); bn.Name = "btnDelete"; bn.Text = "Delete"; bn.Size = new System.Drawing.Size(100, 50); myTabPage.Controls.Add(bn); I have positioned the button, what property would I use to

  • how to create a chm help file for WPF Application? 2009-09-13

    For a WPF UI application, a CHM Help file needs to be created. How to create a chm help file? First create the document in ms word and convert it into chm help file? or any other method? Please help Thanks Ramm --------------Solutions------------- I

  • How to create a built-in Database for WPF application 2011-04-06

    I am planning on building a C# WPF desktop application with a PostgreSQL database (to store using settings and some textbox data). The user of this application will NOT have a database server on their computer. Can I build a "built-in"/"run-time" dat

  • How to set ItemsPanelTemplate to a dynamically created Grid in code behind 2011-12-06

    I've got this UserControl defined in XAML and would like to set the ItemsPanelTemplate dynamically in my code behind class (not in the XAML like in the example): <UserControl> <ItemsControl x:Name="Items"> <ItemsControl.ItemsPanel>

  • Fluent NHibernate Classes as code behind for Domain classes 2009-05-08

    Ok, so I want to create a code behind for non-aspx classes. I have mapping files (classes) that I want to show in project as a code behind for entites. Is there a way to do that in VS2008. --------------Solutions------------- you can create a designe

  • Custom Code Behind for DataSet 2009-11-12

    I used to be able to double-click the work area of a dataset to create and load a code-behind page for it, where i could define some custom code to modify or extend the functionality. I don't know if it's because I'm using VS 2008, but now that doesn

  • Binding a jQuery event to code created in ASPX code-behind? 2011-05-05

    For reasons out of my control I have both client-side JavaScript that builds up a row of an HTML table and similar code in the code-behind page that triggers after a button event. On the client-side, there is an extra method that is called after the

  • Placing code behind for a DNN page 2009-06-16

    I'm having troubles finding where I can put code-behind for my dnn pages. For example: MyPage.ascx already has <%@ Control language="vb" CodeBehind="~/admin/Skins/skin.vb" AutoEventWireup="false" Explicit="True" Inherits="DotNetNuke.UI.Skins.Skin"

  • How to bind to a "x:static" resource in code-behind in WPF 2009-08-26

    For some user controls, I have this binding: AppLanguage="{Binding Path=ApplicationLanguage, Source={x:Static Application.Current}}" This works for controls that are declared/instantiated in XAML. However, I have a control that is only instantiated d

  • How to create Simple HLSL Silverlight filter for blending/playing with/mixing 2 images? 2009-11-15

    How to create Simple HLSL Silverlight filter for blending/playing with/mixing 2 images? I need some working example of a filter which would take as an input 2 images\objects and return 1 image - result of some calculations. I want to bring to Silverl

  • how to create a new joomla component for integrating the google map api 2010-06-04

    how to create a new joomla component for integrating the google map api --------------Solutions------------- Not sure what else you could need that wouldn't be covered by http://tech.reumer.net/google-maps/documentation-of-plugin-googlemap/welcome.ht

  • How can i call a code behind function when user clicks a gridview row? 2011-05-07

    I have a gridview in an aspx page with c# code behind. Is there a way how i can run a code behind function when the user clicks anywhere on a row? For now, i use the select button. But then the user has to click on that button. And i want to user to

  • How to create pause or delay in FOR loop? 2012-04-07

    I am working on a website, where I need to create a pause or delay. So please tell me How to create pause or delay in for loop in javascript or jQuery This is a test example var s = document.getElementById("div1"); for (i = 0; i < 10; i++) { s.inn

  • Code-behind for DataTemplate defined in a ResourceDictionary 2009-07-10

    I've defined a DataTemplate in a ResourceDictionary. The template need some data (for populating a ListBox). Previously the template was a UserControl and the data was provided by setting the DataContext property. Is there some way of using code-behi

  • How to create Net.TCP WCF binding for\using SOAP 1.1? 2009-11-07

    How to create Net.TCP WCF binding for\using SOAP 1.1? Is it posible? If yes. What do I need? MSDN if there is any help on this. Open Source Libs/wrappers. Tutorials and blog articles on How to do it. If no: I need know where did you get the informati

  • Accessing Code Behind for a Control in a Master Page 2010-08-12

    I need to display a control consistently across a set of pages. So I'm using a MasterPage to do that in ASP.NET/C#. However I also need to programmatically access this control, mostly provide options to view/hide depending on whether the controls che

  • While running some java DB Unit tests which call a python script how can I test the code coverage for the python script? 2010-09-20

    I have a python script which generates some reports based on a DB. I am testing the script using java Db Units which call the python script. My question is how can I verify the code coverage for the python script while I am running the DB Units? ----

  • Code-behind for DataTemplate in a ResourceDictionary 2011-04-12

    I am attempting to use code-behind to support an event handler in a DataTemplate. The below code works fine when it is the code-behind for a Window, but not for a ResourceDictionary. The code will not even compile when put in the code-behind for the

  • How to create dynamic incrementing variable using "for" loop in C# 2011-09-12

    How to create dynamic incrementing variable using "for" loop in C#? like this: track_1, track_2, track_3, track_4. so on. --------------Solutions------------- You can't create dynamically-named variables. All you can do - it to create some collection

  • How to create app with 2 view for different orientations? 2011-12-15

    How to create app with 2 view for different orientations? For landscape and portrait, i need 2 XIB. Own for each orietation? --------------Solutions------------- You can react on orientation changing with willRotateToInterfaceOrientation: Create an a

  • How to create shortcut on my desktop for Facebook/You Tube/Twitter? 2012-08-29

    Original Title: create shortcut on my desktop How to create shortcut on my desktop for Facebook/You Tube/Twitter? --------------Solutions------------- Are they saved as favorites? Drag them to the desktop and you'll have a shortcut. Or, when at the W

  • How to create Windows 7 Firewall rule for javaw.exe? 2013-05-02

    How to create Windows 7 Firewall rule for javaw.exe? The application path differed new versions and user profiles. Are there wildcard symbols allowed e.g. asterisk? --------------Solutions------------- Hi, I would recommend you to post the query in a

  • How to create an objective response sheet (for printing only) in word 2010 2013-10-08

    Original Title: ? 2 create a ORS in microsoft office 2010? how to create an objective response sheet (for printing only) in word 2010? ORS like in some competitive exms. does there any templete from microsoft of ORS? Does i have to do it manually.. i

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.092 (s). 12 q(s)