C# EventTrigger RouteEvent未使用自定义事件触发
我试图在触发自定义路由事件(由我定义)时触发故事板播放。该事件称为CustomTest,在MyControl中定义 虽然正在触发事件,但触发器未播放情节提要 XAML:C# EventTrigger RouteEvent未使用自定义事件触发,c#,wpf,user-controls,triggers,routed-events,C#,Wpf,User Controls,Triggers,Routed Events,我试图在触发自定义路由事件(由我定义)时触发故事板播放。该事件称为CustomTest,在MyControl中定义 虽然正在触发事件,但触发器未播放情节提要 XAML: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:gear="clr-namespace:GearWPF"
x:Class="GearWPF.MyControl"
x:Name="UserControl">
...
<Grid x:Name="LayoutRoot" Background="#00000000">
<Grid.Triggers>
<EventTrigger RoutedEvent="gear:MyControl.CustomTest"> <!-- My custom event. -->
<BeginStoryboard Storyboard="{StaticResource MyStoryBoard}"/>
</EventTrigger>
</Grid.Triggers>
</Grid>
namespace GearWPF
{
/// <summary>
/// Interaction logic for MyControl.xaml
/// </summary>
public partial class MyControl: UserControl
{
// Create a custom routed event by first registering a RoutedEventID
// This event uses the bubbling routing strategy
public static readonly RoutedEvent CustomTestEvent = EventManager.RegisterRoutedEvent("CustomTest", RoutingStrategy.Bubbling, typeof(RoutedEventHandler), typeof(MyControl));
// Provide CLR accessors for the event
public event RoutedEventHandler CustomTest
{
add { AddHandler(CustomTestEvent, value); }
remove { RemoveHandler(CustomTestEvent, value); }
}
public MyControl()
{
this.InitializeComponent();
}
public void RaiseMyEvent()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomTestEvent);
RaiseEvent(newEventArgs);
}
}
}
...
C#:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:gear="clr-namespace:GearWPF"
x:Class="GearWPF.MyControl"
x:Name="UserControl">
...
<Grid x:Name="LayoutRoot" Background="#00000000">
<Grid.Triggers>
<EventTrigger RoutedEvent="gear:MyControl.CustomTest"> <!-- My custom event. -->
<BeginStoryboard Storyboard="{StaticResource MyStoryBoard}"/>
</EventTrigger>
</Grid.Triggers>
</Grid>
namespace GearWPF
{
/// <summary>
/// Interaction logic for MyControl.xaml
/// </summary>
public partial class MyControl: UserControl
{
// Create a custom routed event by first registering a RoutedEventID
// This event uses the bubbling routing strategy
public static readonly RoutedEvent CustomTestEvent = EventManager.RegisterRoutedEvent("CustomTest", RoutingStrategy.Bubbling, typeof(RoutedEventHandler), typeof(MyControl));
// Provide CLR accessors for the event
public event RoutedEventHandler CustomTest
{
add { AddHandler(CustomTestEvent, value); }
remove { RemoveHandler(CustomTestEvent, value); }
}
public MyControl()
{
this.InitializeComponent();
}
public void RaiseMyEvent()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomTestEvent);
RaiseEvent(newEventArgs);
}
}
}
wpf
{
///
///MyControl.xaml的交互逻辑
///
公共部分类MyControl:UserControl
{
//通过首先注册RoutedEventID创建自定义路由事件
//此事件使用冒泡路由策略
公共静态只读RoutedEvent CustomTestEvent=EventManager.RegisterRoutedEvent(“CustomTest”,RoutingStrategy.Bubbling,typeof(RoutedEventHandler),typeof(MyControl));
//为事件提供CLR访问器
公共事件路由EventHandler自定义测试
{
添加{AddHandler(CustomTestEvent,value);}
删除{RemoveHandler(CustomTestEvent,value);}
}
公共MyControl()
{
this.InitializeComponent();
}
公共无效的RaiseMyEvent()
{
RoutedEventTargets newEventArgs=新RoutedEventTargets(CustomTestEvent);
RaiseEvent(newEventArgs);
}
}
}
我已经验证了RaiseMyEvent在我期望的时候被调用。使用Snoop,我可以看到事件一直在我的控制之下(处理为“False”)。但是,触发器实际上并没有启动故事板
我还将触发器更改为使用现有事件,当我这样做时,故事板将被触发。这让我相信这是我的路由事件CustomTest特有的东西
<Grid x:Name="LayoutRoot" Background="#00000000">
<Grid.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter"> <!-- This works! -->
<BeginStoryboard Storyboard="{StaticResource MyStoryBoard}"/>
</EventTrigger>
</Grid.Triggers>
</Grid>
问题在于事件的级别太高。我将它发送到我的CustomControl,但我真正想做的是将它发送到CustomControl中的网格(因为事件只在根和源之间进行)
我在main window.xaml.cs中有自己的RouteEvent,并且我的EventTrigger没有启动。你的回答帮助了我,因为我把它放在了我的格子里,太低了,无法参加比赛!