Registering RoutedEventHandler in UWP template control

1.2k Views Asked by At

I'm having difficulty finding how to register a RoutedEventHandler in UWP. I'm attempting to code a template control that has event properties similar to ContentDialog's:

PrimaryButtonClick="ClickEvent"

Where ClickEvent is defined in the cs file. I'm only just getting the hang of templates, but I believe I want to do something that looks like this:

<Button Content="{TemplateBinding PrimaryButtonText}" Click="{TemplateBinding PrimaryButtonClick}"/>

Currently, all I can find is references to WPF versions of this type of code:

 public static readonly RoutedEvent ValueChangedEvent =
        EventManager.RegisterRoutedEvent("ValueChanged",
RoutingStrategy.Direct, typeof(RoutedPropertyChangedEventHandler<double>),
typeof(NumericBox));

    public event RoutedPropertyChangedEventHandler<double> ValueChanged
    {
        add { AddHandler(ValueChangedEvent, value); }
        remove { RemoveHandler(ValueChangedEvent, value); }
    }

    private void OnValueChanged(double oldValue, double newValue)
    {
        RoutedPropertyChangedEventArgs<double> args =
    new RoutedPropertyChangedEventArgs<double>(oldValue, newValue);
        args.RoutedEvent = NumericBox.ValueChangedEvent;
        RaiseEvent(args);
    }

But of course the types have changed. Can someone point me in the right direction?

1

There are 1 best solutions below

5
On

Unfortunately, the concept of RoutedEvent (bubbling, tunneling) is not available in UWP currently. You can just create a classic event however instead:

public event EventHandler PrimaryButtonClick;

protected void InnerButton_Click(object sender, EventArgs e)
{
    PrimaryButtonClick?.Invoke( sender, e );              
}

Bubbling of events is possible for some predefined events, but it is not yet possible to allow bubbling for custom events in current version of UWP.