I am trying to trigger a storyboard to play when a custom routed event (defined by me) is fired. The event is called CustomTest and is defined in MyControl.

Although the event is being fired, the trigger is not playing the storyboard.

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);
        }
    }
}

I have verified that RaiseMyEvent is called when I expect it to. And using Snoop, I can see that the event is making it all the way down to my control (where handled is "False"). However, the trigger is not actually starting the storyboard.

I have also changed the Trigger to use an existing event, and when I do that, the storyboard is triggered. This leads me to believe it is something specific to my Routed Event, CustomTest.

    <Grid x:Name="LayoutRoot" Background="#00000000">
        <Grid.Triggers>
            <EventTrigger RoutedEvent="Mouse.MouseEnter"> <!-- This works! -->
                <BeginStoryboard Storyboard="{StaticResource MyStoryBoard}"/>
            </EventTrigger>
        </Grid.Triggers>
    </Grid>
有帮助吗?

解决方案

The issue was that the event was being Raises at too High a level. I was sending it to my CustomControl, but what I really wanted to do was send it to my Grid within the CustomControl (since events only go between the root and the source).

    public void RaiseMyEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(CustomTestEvent);
        LayoutRoot.RaiseEvent(newEventArgs); // This change fixes the issue.
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top