如何在控件模板中捕获事件 [英] How to catch the event in the control template

查看:70
本文介绍了如何在控件模板中捕获事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的自定义控件,并向路由事件添加了一个简单的情节提要,我想在情节提要完成后做一些事情,但是会发生错误,如下所示:
CustomControl1.cs代码:

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代码:



Generic.xaml codes:

<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>



错误发生在此行:Storyboard Completed ="Storyboard_Completed".
似乎我无法像其他控件一样添加事件(如Button),并且错误信息是:
"ResourceDictionary"根元素需要一个x:Class属性来支持XAML文件中的事件处理程序.删除Completed事件的事件处理程序,或将x:Class属性添加到根元素.第21行的位置45.

如何处理此错误并达到我的目的(在过程代码中的情节提要完成后执行一些操作)?
感谢您的帮助.



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.

推荐答案

您无法在控件模板中捕获"该事件.
相反,您必须命名情节提要,并通过重写控件中的OnApplyTemplate方法来检索应用于控件的情节提要实例.

如果您获得了情节提要实例,那么您的控件可能会引发另一个事件
通知已订阅原始事件的侦听器.

要获得更好的解释,请点击此处:链接

希望这会有所帮助.

再见,

托马斯.
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.


这篇关于如何在控件模板中捕获事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆