【发布时间】:2014-03-11 20:44:13
【问题描述】:
我试图在触发自定义路由事件(由我定义)时触发故事板播放。该事件称为 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,我可以看到该事件一直到我的控制(其中处理的是“假”)。但是,触发器实际上并未启动情节提要。
我还更改了触发器以使用现有事件,当我这样做时,情节提要被触发。这让我相信它是特定于我的路由事件 CustomTest。
<Grid x:Name="LayoutRoot" Background="#00000000">
<Grid.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter"> <!-- This works! -->
<BeginStoryboard Storyboard="{StaticResource MyStoryBoard}"/>
</EventTrigger>
</Grid.Triggers>
</Grid>
【问题讨论】:
标签: c# wpf user-controls triggers routed-events