4

Rather than use an event generated by an input device, I want to use a custom event that will be raised programatically in the code-behind as an EventTrigger in my xaml.
This should be laughably easy but I can't find an example anywhere.

Here's what I've come up with from studying WPF4 Unleashed Chapter 6, Routed Event Implementation, EventTrigger.RoutedEvent Property, Custom RoutedEvent as EventTrigger, and many others:

MainWindow.xaml.cs:

namespace RoutedEventTrigger
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            RaiseEvent(new RoutedEventArgs(fooEvent, this));
        }
        public static readonly RoutedEvent fooEvent = EventManager.RegisterRoutedEvent(
        "foo", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(MainWindow));

        // Provide CLR accessors for the event 
        public event RoutedEventHandler foo
        {
            add { AddHandler(fooEvent, value); }
            remove { RemoveHandler(fooEvent, value); }
        }
    }
}

MainWindow.xaml:

MainWindow.xaml

P.S. Please be gentle, I am relatively new to WPF.

Community
  • 1
  • 1
Scott Solmer
  • 3,871
  • 6
  • 44
  • 72

3 Answers3

6

Okuma Scott,

Have you tried to build (rebuild) the project. WPF requires you to build the project in order for the project changes to be visible to the XAML parser. Using the code below builds just fine.

Code

public partial class MainWindow : Window
{
    public MainWindow() => InitializeComponent();

    public static readonly RoutedEvent fooEvent = EventManager.RegisterRoutedEvent("foo",
        RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(MainWindow));

    // Provide CLR accessors for the event 
    public event RoutedEventHandler foo
    {
        add => AddHandler(fooEvent, value);
        remove => RemoveHandler(fooEvent, value);
    }
}

XAML.

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">
    <Window.Triggers>
        <EventTrigger RoutedEvent="local:MainWindow.foo" />
    </Window.Triggers>
</Window>

Edit: The same parser error was displayed until the project was re-built.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Nico
  • 12,493
  • 5
  • 42
  • 62
  • I didn't think to try that, but it worked! Unbelievable. And here I thought that changes made to the xaml were visible immediately. I figured I must have been doing something really wrong because the preview window wouldn't display, citing "invalid markup" and simply building the project did not remedy this. I actually had to (Clean Solution) --> (Rebuild) to clear the error. Thanks! – Scott Solmer Jan 10 '14 at 12:47
  • Reboot, Clean, Rebuild nothing works for me! It builds and runs perfectly, but the designer fails. Argh. – Ciantic Oct 11 '14 at 17:37
2
<Window.Triggers>
    <EventTrigger RoutedEvent="{x:Static local:MainWindow.foo}" />
</Window.Triggers>

I ran into the same problem, but all your solutions didn't work for me. This snippet above did solve the issue for me.

Taelia
  • 591
  • 3
  • 20
0

I had the same problem but rebuilding didn't work for me untill i restardet my Studio. Closed it and reopened the Projekt and it worked fine.

Phillip
  • 789
  • 4
  • 22