Click here to Skip to main content
11,715,160 members (63,713 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WPF
I have created a simple custom control, and add a simple storyboard to a routed event, I want to do something after the storyboard is completed, but error happens, as below:
CustomControl1.cs codes:
public class CustomControl1 : Control
        static CustomControl1()
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));

Generic.xaml codes:
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                        <Grid x:Name="TheGrid" Width="100" Height="100" removed="LightBlue">
                                <ScaleTransform CenterX="50" CenterY="50" ScaleX="1" ScaleY="1"/>
                        <EventTrigger RoutedEvent="Control.MouseEnter">
                                <Storyboard Completed="Storyboard_Completed">
                                    <DoubleAnimation Storyboard.TargetName="TheGrid"
                                                     From="1" To="1.25" Duration="0:0:.5"/>
                                    <DoubleAnimation Storyboard.TargetName="TheGrid"
                                                     From="1" To="1.25" Duration="0:0:.5"/>
                                    <DoubleAnimation Storyboard.TargetName="TheGrid"
                                                     From="1.25" To="1" Duration="0:0:.5" BeginTime="0:0:.5"/>
                                    <DoubleAnimation Storyboard.TargetName="TheGrid"
                                                     From="1.25" To="1" Duration="0:0:.5" BeginTime="0:0:.5"/>

Error happens at this line:Storyboard Completed="Storyboard_Completed".
It seems as if I can't add the event like in the other controls(as Button), and the error information is :
'ResourceDictionary' root element requires a x:Class attribute to support event handlers in the XAML file. Either remove the event handler for the Completed event, or add a x:Class attribute to the root element. Line 21 Position 45.

How to deal with this error and get my purpose(do something after the storyboard completed in the procedure code)?
Thanks for your help.
Posted 14-Aug-12 15:27pm
Edited 29-Aug-12 0:32am
Sergey Alexandrovich Kryukov at 15-Aug-12 0:08am
To start with, what do you mean by "catching event"? :-)
RenZan at 15-Aug-12 2:09am
Maybe I didn't express well, I meens when the "Completed" event of Storyboard happening, I want do something in the procedure codes(.cs), such as, if I don't use control template, just use the normal control, .xaml codes as below(sorry I don't know how to dispaly the angle brackets):
Window x:Class="ControlTemplateEvents.Window1"
Title="Window1" Height="300" Width="300"
Grid x:Name="theGrid" Width="50" Height="50" Background="LightBlue"
EventTrigger RoutedEvent="Grid.MouseEnter"
Storyboard Completed="Storyboard_Completed"
DoubleAnimation Storyboard.TargetName="theGrid" Storyboard.TargetProperty="Height"
To="100" Duration="0:0:1"/
and the .cs codes as below:
public partial class Window1 : Window
public Window1()

private void Storyboard_Completed(object sender, EventArgs e)
when the storyboard is completed, I can show the message.
how can I do this in a control template?
thank you very much!

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can't 'catch' the event in a control template.
Instead you have to name the storyboard and retrieve the storyboard instance applied to your control by overriding the OnApplyTemplate method in your control.

If you got the storyboard instance then your control could raise another event
notifiying subscribed listeners of the original event.

For a better explanation look here : Link

Hope this helps.



This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 495
1 Richard MacCutchan 275
2 Maciej Los 259
3 ManasKumarM 155
4 Frankie-C 125
0 Sergey Alexandrovich Kryukov 775
1 CHill60 415
2 Richard MacCutchan 405
3 Maciej Los 389
4 OriginalGriff 320

Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 29 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100