Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
public class CustomControl1 : Control
    {
        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }
    }


Generic.xaml codes:
XML
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ControlTemplateEvents">

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


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
Updated 29-Aug-12 0:32am
v2
Comments
Sergey Alexandrovich Kryukov 15-Aug-12 0:08am    
To start with, what do you mean by "catching event"? :-)
--SA
RenZan 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"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
Grid x:Name="theGrid" Width="50" Height="50" Background="LightBlue"
Grid.Triggers
EventTrigger RoutedEvent="Grid.MouseEnter"
BeginStoryboard
Storyboard Completed="Storyboard_Completed"
DoubleAnimation Storyboard.TargetName="theGrid" Storyboard.TargetProperty="Height"
To="100" Duration="0:0:1"/
/Storyboard
/BeginStoryboard
/EventTrigger
/Grid.Triggers
/Grid
/Window
and the .cs codes as below:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void Storyboard_Completed(object sender, EventArgs e)
{
MessageBox.Show("Completed!");
}
}
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

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.

Bye,

Thomas.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900