0

I have a WPF MainWindow with my own RoutedEvent, which is raised when pushing the Enter key.

namespace ZK16
{
public partial class MainWindow : Window
{

    public static readonly RoutedEvent CustomEvent = EventManager.RegisterRoutedEvent(
        "Custom", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MainWindow));

    public event RoutedEventHandler Custom
    {
        add { AddHandler(CustomEvent, value); }
        remove { RemoveHandler(CustomEvent, value); }
    }

    void RaiseCustomEvent()
    {
        Debug.WriteLine("RaiseCustomEvent");
        RoutedEventArgs newEventArgs = new RoutedEventArgs(MainWindow.CustomEvent);
        RaiseEvent(newEventArgs);
    }

    public MainWindow()
    {
        InitializeComponent();
        this.AddHandler(MainWindow.CustomEvent, new RoutedEventHandler(MyCustomHandler));
    }

    private void MyCustomHandler(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("In Eventhandler...");
    }

    private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
            RaiseCustomEvent();
    }

In XAML I add a Label with an EventTrigger,

<Window x:Class="ZK16.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ZK16"
    Title="MainWindow"
    PreviewKeyDown="Window_PreviewKeyDown"
    >
<Grid>
    <Label Content="Hello World">
        <Label.Triggers>
            <EventTrigger RoutedEvent="local:MainWindow.Custom">
                <BeginStoryboard>
                    <Storyboard Duration="00:00:1">
                        <DoubleAnimation From="6" To="25" Storyboard.TargetProperty="FontSize"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Label.Triggers>
    </Label>
</Grid>
</Window>

but when raising the custom event, the EventTrigger does not fire on my event, but fires if e.g.

EventTrigger RoutedEvent="Loaded"

Can anybody tell me, what's wrong?

x y
  • 911
  • 9
  • 27

1 Answers1

3

I think there is a minunderstanding of what a routed event is.

This MSDN article states the following:

Bubbling: Event handlers on the event source are invoked. The routed event then routes to successive parent elements until reaching the element tree root...

Tunneling: Initially, event handlers at the element tree root are invoked. The routed event then travels a route through successive child elements along the route, towards the node element that is the routed event source (the element that raised the routed event)...

In your case you have a bubbling event. You raise it in the MainWindow (the source) so it will start going from that element upwards. Nothing is above MainWindow so it stops there.

If you change your event to a tunneling event this would happen. You raise it from MainWindow (the source) so the event starts travelling from the parents to the source, again, nothing is above MainWindow, same result.

Now if you do the following in your XAML:

<Label x:Name="MyLabel" Content="Hello World">

And this in your code:

MyLabel.RaiseEvent(newEventArgs);

The route will be the following in a bubbling event's case:

Label (source) -> Grid -> MainWindow

And in a tunneling event's case:

MainWindow -> Grid -> Label (source)

This person had the same problem here.

Community
  • 1
  • 1
Szabolcs Dézsi
  • 8,743
  • 21
  • 29