将事件设置为所有相同类型的控件 [英] Set an event to all controls of same type
本文介绍了将事件设置为所有相同类型的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在WPF中,有一种方法可以为特定类型的所有控件设置特定类型的事件。例如,我试图在所有TextBlock类型的控件上设置MouseLeftButtonDown事件。这是我尝试做的事情,这显然是错误的:
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?
推荐答案
是的,有一种方法,您可以在 Style 中使用
为所有应用了该样式的 EventSetter
属性 Elements
设置一个 EventHandler
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
示例:
<Style TargetType="{x:Type TextBlock}">
<EventSetter Event="MouseLeftButtonDown" Handler="TextBlock_LeftClick" />
</Style>
这篇关于将事件设置为所有相同类型的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文