如何设置通过风格的事件函数? [英] How to set an event function via a style?

查看:115
本文介绍了如何设置通过风格的事件函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些是应该产生同样的动作(代码隐藏函数调用)的鼠标交互(的MouseEnter,鼠标离开)。结果
我几个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" />



由于在本实施例的许多控制呼叫的相同的两个函数GeneralMouseEnter和GeneralMouseLeave,我。倒是希望能够定义一个风格或者类似的封装这一行为的东西。

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.

结果
这是应该成为一种插件以后的。结果
(包括代码和XAML文件的任何GUI程序和设置的样式每个交互控制元素...)

[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...)

这是我在网上找到,我可以使用 EventTriggers 就像这个例子:

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.


  • 是否有可能通过创建与动作+触发风格应用于每个控制得到这个功能呢?那怎么办?

  • 我如何分配多个样式(多重交互事件),以一个控制?

  • 是否有甚至是一个更清洁的方式实现这种行为?

  • 结果
    如果我想,让我们说,对我的GUI所有滑块鼠标交互?

  • 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?

推荐答案

马丁,

您可以直接从样式使用EventSetter分配事件处理程序:

you can assign an event handler directly from a style using an EventSetter:

<Style TargetType="{x:Type Button}">
  <EventSetter Event="Click" Handler="SomeAction"/>
</Style>



@ColinE:

@ColinE:

我不知道该用一种风格来执行事件线就是个不错的主意。花柱,根据定义,定义的控制视觉外观

I am not sure that using a style to perform event wire-up is a good idea. Styles, by definition, define the visual appearance of controls.

不幸的是,这似乎是大约WPF样式的普遍和广泛的误解:虽然他们的名字所暗示的他们,就像你说的话,只是为了界定的视觉外观,他们实际上还有更多:它有利于更广泛地查看样式作为快捷方式来控制分配一组属性。

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.

这篇关于如何设置通过风格的事件函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆