4

I have a file resdict.xaml which is resource dictionary

This file is linked in my window xaml file window.xaml.cs:

<Window.Resources> <ResourceDictionary Source="resdict.xaml" /> <Window.Resources> 

Now, in the window.xaml.cs i also have this code for a storyboard:

<Storyboard Completed="HandlingEventHandler"> ....some code... </Storyboard> 

When the storyboard is used and completes its run, it triggers HandlingEventHandler(). This is what i want.

Now, we move the Storyboard code into the resource dictionary file resdict.xaml.

I can still use the storyboard fine, it does its thing, it plays the animations and all that, but the HandlingEventHandler is no longer triggered. Why is that?

Is there some way to remedy the situation without having to create partial class for the dictionary file?

For example, if i make the HandlingEventHandler static can i do something like:

<Storyboard Completed="{Static: Myclass.HandlingEventHandler}"> 

?

3

1 Answer 1

0

Yes, you can use a custom attached behaviour.

Here's a simple example of how to attach the completed event:

public class StoryBoardBehaviour : DependencyObject { public static readonly DependencyProperty AttachCompletedHandlerProperty = DependencyProperty.Register("AttachCompletedHandler", typeof(bool), typeof(StoryBoardBehaviour), new PropertyMetadata(false, AttachCompletedHandlerChanged)); public static void SetAttachCompletedHandler(DependencyObject obj, bool value) { obj.SetValue(AttachCompletedHandlerProperty, value); } public static bool GetAttachCompletedHandler(DependencyObject obj) { return (bool)obj.GetValue(AttachCompletedHandlerProperty); } private static void AttachCompletedHandlerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var storyboard = (Storyboard)d; var oldValue = (bool)e.OldValue; var newValue = (bool)e.NewValue; if (newValue && !oldValue) { storyboard.Completed += StoryboardOnCompleted; } if (!newValue && oldValue) { storyboard.Completed -= StoryboardOnCompleted; } } private static void StoryboardOnCompleted(object sender, object o) { // Completed handler logic } } 

Then in your Storyboard definition in your resource dictionary, add use the attached behaviour as follows:

<Storyboard behaviours:StoryBoardBehaviour.AttachCompletedHandler="True">

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.