Search code examples
c#wpfborderrouted-eventstemplatebinding

Add RoutedEvent via TemplateBinding


I want to use a RoutedEvent on a Border in a XAML Dictionary. The RoutedEvent comes from the class for which the template is, how can I achieve this?

ModernWindow.cs

/// <summary>
/// Gets fired when the logo is clicked.
/// </summary>
public static readonly RoutedEvent LogoClickEvent = EventManager.RegisterRoutedEvent("LogoClickRoutedEventHandler", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ModernWindow));

/// <summary>
/// The routedeventhandler for LogoClick
/// </summary>
public event RoutedEventHandler LogoClick 
{
    add { AddHandler(LogoClickEvent, value); }
    remove { RemoveHandler(LogoClickEvent, value); }
}

/// <summary>
/// 
/// </summary>
protected virtual void OnLogoClick() 
{
    RaiseEvent(new RoutedEventArgs(LogoClickEvent, this));
}

ModernWindow.xaml

<!-- logo -->
<Border MouseLeftButtonDown="{TemplateBinding LogoClick}" Background="{DynamicResource Accent}" Width="36" Height="36" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,76,0">
    <Image Source="{TemplateBinding Logo}" Stretch="UniformToFill" />
</Border>

Solution

  • I found finally a solution, I used InputBindings and then Commands.

    <Border.InputBindings>
        <MouseBinding Command="presentation:Commands.LogoClickCommand" Gesture="LeftClick" />
    </Border.InputBindings>
    

    Its not that what I wanted, but it works :)