将RoutedEvent自定义为EventTrigger

本文关键字:EventTrigger 自定义 RoutedEvent | 更新日期: 2023-09-27 18:01:14

我有自己的形状类

public sealed class MirrorTile : Shape

在这个类中,我添加了事件

public static readonly RoutedEvent SelectedEnterEvent = EventManager.RegisterRoutedEvent("SelectedEnter", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MirrorTile));
public event RoutedEventHandler SelectedEnter
{
    add
    {
        this.AddHandler(SelectedEnterEvent, value);
    }
    remove
    {
        this.RemoveHandler(SelectedEnterEvent, value);
    }
}

并希望以这种方式使用

<shapes:MirrorTile>
    <shapes:MirrorTile.Triggers>
        <EventTrigger RoutedEvent="SelectedEnter">
            <BeginStoryboard Storyboard="{StaticResource SelectShape}"/>
        </EventTrigger>
    </shapes:MirrorTile.Triggers>
</shapes:MirrorTile>

starup之后,我得到了一个例外:{"RoutedEventConverter cannot convert from System.String."}

我做错了什么?我该如何解决这个问题?

将RoutedEvent自定义为EventTrigger

<EventTrigger RoutedEvent="shapes:MirrorTile.SelectedLeave">

命名空间也丢失了。

您还必须提供类型:

<EventTrigger RoutedEvent="MirrorTile.SelectedEnter"></EventTrigger>

评论后编辑:

您是否尝试将命名空间添加到XAML声明中?

 xmlns:local="clr-namespace:YourNameSpace"

然后将其修复为:

 <EventTrigger RoutedEvent="local:MirrorTile.SelectedEnter"></EventTrigger>

我认为您缺少定义事件的类型:

<EventTrigger RoutedEvent="MirrorTile.SelectedEnter">