我试图在触发自定义路由事件(由我定义)时触发故事板。该事件称为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://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>
C#:
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);
}
}
}
我已经确认在我期望它时调用RaiseMyEvent。使用Snoop,我可以看到事件正在一直到我的控制(处理的是#34; False&#34;)。但是,触发器实际上并没有启动故事板。
我还更改了Trigger以使用现有事件,当我这样做时,会触发故事板。这让我相信它是特定于我的路由事件CustomTest。
<Grid x:Name="LayoutRoot" Background="#00000000">
<Grid.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter"> <!-- This works! -->
<BeginStoryboard Storyboard="{StaticResource MyStoryBoard}"/>
</EventTrigger>
</Grid.Triggers>
</Grid>
答案 0 :(得分:2)
问题是该事件的升级程度太高了。我将它发送到我的CustomControl,但我真正想做的是将它发送到 CustomControl中的Grid (因为事件只在根和源之间)。
public void RaiseMyEvent()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomTestEvent);
LayoutRoot.RaiseEvent(newEventArgs); // This change fixes the issue.
}