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?
Yes there is a way, you can use the
EventSetter
property in theStyle
to set aEventHandler
for allElements
with that style appliedExample:
You can do this in a single statement using WPF EventManager