Click here to Skip to main content
15,895,746 members

How to catch the event in the control template

RenZan asked:

Open original thread
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.
Tags: C#, WPF

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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