Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set an event to all controls of same type

In WPF is there a way to set a particular type of event to all controls of the same type. For example, I'm trying to set the MouseLeftButtonDown event on all controls of type TextBlock. Here is what I tried to do which is obviously wrong:

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="MouseLeftButtonDown" Value="TextBlock_LeftClick"/>
</Style>

Is there a way that this can be done?

like image 448
Ikeem Wilson Avatar asked Dec 21 '22 09:12

Ikeem Wilson


1 Answers

Yes there is a way, you can use the EventSetter property in the Style to set a EventHandler for all Elements with that style applied

Example:

<Style TargetType="{x:Type TextBlock}">
    <EventSetter Event="MouseLeftButtonDown" Handler="TextBlock_LeftClick" />
</Style>
like image 144
sa_ddam213 Avatar answered Jan 05 '23 01:01

sa_ddam213