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 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With