Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Is it possible to assign an event in a Style?

Tags:

events

wpf

xaml

Specifically I want all PasswordBox's using a Style to have the same static KeyDown event handler - would it be possible to somehow set this in the Style they are using rather than setting it on each PasswordBox? (which I am having to do in code behind at the moment as they all point to the same static handler)..

like image 211
Lara Avatar asked May 18 '11 10:05

Lara


People also ask

What is routed event in WPF?

A routed event is an event registered with the WPF event system, backed by an instance of the RoutedEvent class, and processed by the WPF event system. The RoutedEvent instance, obtained from registration, is typically stored as a public static readonly member of the class that registered it.


1 Answers

EventSetter is your friend. As for the handler - just put it into code-behind (you can create a code-behind for a resource dictionary too - just crate a CS file with the same name as the dictionary and add the class attribute to the resource dictionary XAML).

Small example:

<Style>
    <EventSetter Event="KeyDown" Handler="KeyDownHandler"/>
</Style>
like image 155
Jefim Avatar answered Nov 16 '22 02:11

Jefim