How to set an event function via a style?

后端 未结 1 1919
南笙
南笙 2020-12-15 02:40

I have several GUI control elements of which some are supposed to generate the same action (code-behind function call) on mouse interaction (MouseEnter, MouseLeave).
[ed

相关标签:
1条回答
  • 2020-12-15 03:40

    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.

    0 讨论(0)
提交回复
热议问题