如何通过样式设置事件函数?
我有几个 GUI 控制元素,其中一些元素应该在鼠标交互(MouseEnter、MouseLeave)上生成相同的操作(代码隐藏函数调用)。
[编辑] 我正在事件处理程序中执行一些与样式无关的功能。
现在我在每个控件中使用事件属性:
<Button Name="Button" Content="Button 1"
MouseEnter="GeneralMouseEnter" MouseLeave="GeneralMouseLeave"
PreviewMouseDown="Button1_PreviewMouseDown" PreviewMouseUp="Button1_PreviewMouseUp" />
<Button Name="NotInteractingButton" Content="Button 2"
/><!-- this button has no MouseOver-effects -->
<ToggleButton Content="ToggleButton"
MouseEnter="GeneralMouseEnter" MouseLeave="GeneralMouseLeave" />
<!-- needs to use IsMouseDirectlyOver on the slider knob... -->
<Slider Name="HorizontalSlider"
MouseEnter="GeneralMouseEnter" MouseLeave="GeneralMouseLeave"
ValueChanged="Slider_ValueChanged" />
<Slider Name="VerticalSlider" Orientation="Vertical"
MouseEnter="GeneralMouseEnter" MouseLeave="GeneralMouseLeave"
ValueChanged="Slider_ValueChanged" />
由于此示例中的许多控件都调用相同的两个函数“GeneralMouseEnter”和“GeneralMouseLeave”,所以我希望能够定义一种样式或类似的东西来封装该行为。
[编辑-澄清]
这应该会成为一种插件。
(将代码和 XAML 文件包含到任何 GUI 程序中,并在每个交互式控制元素上设置样式...)
根据我在网上找到的内容,我可以使用 EventTriggers
就像本例中一样:
<Style.Triggers>
<EventTrigger RoutedEvent="Click">
<EventTrigger.Actions>
<BeginAction TargetName="SomeAction" />
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
我不'但不知道是否以及如何在操作中调用函数。
- 是否可以通过创建一个带有动作+触发器的样式来应用到每个控件来获得此功能?怎么做呢?
- 如何为一个控件分配多种样式(针对多个交互事件)?
- 是否有更干净的方法来实现这种行为?
- [编辑]
如果我想在 GUI 中的所有滑块上进行鼠标交互怎么办?
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).
[edit] I am performing some non style related functionality in my event handlers.
Right now I'm using event attributes in each control:
<Button Name="Button" Content="Button 1"
MouseEnter="GeneralMouseEnter" MouseLeave="GeneralMouseLeave"
PreviewMouseDown="Button1_PreviewMouseDown" PreviewMouseUp="Button1_PreviewMouseUp" />
<Button Name="NotInteractingButton" Content="Button 2"
/><!-- this button has no MouseOver-effects -->
<ToggleButton Content="ToggleButton"
MouseEnter="GeneralMouseEnter" MouseLeave="GeneralMouseLeave" />
<!-- needs to use IsMouseDirectlyOver on the slider knob... -->
<Slider Name="HorizontalSlider"
MouseEnter="GeneralMouseEnter" MouseLeave="GeneralMouseLeave"
ValueChanged="Slider_ValueChanged" />
<Slider Name="VerticalSlider" Orientation="Vertical"
MouseEnter="GeneralMouseEnter" MouseLeave="GeneralMouseLeave"
ValueChanged="Slider_ValueChanged" />
Since many controls in this example are calling the same two functions "GeneralMouseEnter" and "GeneralMouseLeave", I'd like to be able to define a style or something similar to encapsulate that behavior.
[edit - clarification]
This is supposed to become a kind of plugin later on.
(Include code and XAML files to any GUI program and set a style on each interactive control element...)
From what I found on the web, I can use EventTriggers
like in this example:
<Style.Triggers>
<EventTrigger RoutedEvent="Click">
<EventTrigger.Actions>
<BeginAction TargetName="SomeAction" />
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
I don't know though if and how to call functions within an action.
- Is it possible to get this functionality by creating a style with action + trigger to be applied to each control? How to do that?
- How do I assign multiple styles (for multiple interaction events) to one control?
- Is there maybe even a cleaner way to achieve this behavior?
- [edit]
What if I want to, let's say, have mouse interaction on all sliders in my GUI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Martin,
您可以使用 EventSetter 从样式直接分配事件处理程序:
@ColinE:
不幸的是,这似乎是对 WPF 样式的一个常见且广泛的误解:虽然它们的名字表明它们就像您所说的那样,只是为了定义视觉外观,但它们实际上有更多的意义:将样式更普遍地视为用于将一组属性分配给控件的快捷方式。
Martin,
you can assign an event handler directly from a style using an EventSetter:
@ColinE:
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.