How to set an event function via a style? How to set an event function via a style? wpf wpf

How to set an event function via a style?


Martin,

you can assign an event handler directly from a style using an EventSetter:

<Style TargetType="{x:Type Button}">  <EventSetter Event="Click" Handler="SomeAction"/></Style>

@ColinE:

I am not sure that using a style to perform event wire-up is a good idea. Styles, by definition, define the visual appearance of controls.

Unfortunately, this seems to be a common and widespread misconception about WPF styles: Although their name suggests they are, like what you say, merely meant to define the visual appearance, they are actually much more: It is helpful to view styles more generally as a shortcut for assigning a set of properties to a control.