在WPF中,是否可以为单个事件触发器指定多个路由事件?

发布于 2024-07-15 09:01:34 字数 258 浏览 4 评论 0原文

我有一个事件触发器,我想触发它​​来响应两个不同的路由事件。 我不想重复事件响应代码(在 XAML 中)两次。 我可以为单个事件触发器声明指定多个路由事件吗?

单个事件的示例:

<Style.Triggers>
    <EventTrigger RoutedEvent="Button.MouseEnter">
        <--XAML MAGIC-->
        ...

I have an event trigger that I want to be fired in response to two different routed events. I don't want to repeat the event response code (in XAML) twice. Can I specify multiple routed events for a single event trigger declaration?

Example of a single event:

<Style.Triggers>
    <EventTrigger RoutedEvent="Button.MouseEnter">
        <--XAML MAGIC-->
        ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泡沫很甜 2024-07-22 09:01:34

抱歉... WPF 的 EventTrigger 实现仅允许一个路由事件。

通常,您会从路由事件中触发故事板。 我建议采取以下折衷方案:

<!--Define a storyboard as a resource-->
<Storyboard x:Key="MyStoryboard1">
   <!--Many properties and etc...-->
</Storyboard>

<Style.Triggers>
   <EventTrigger RoutedEvent="Button.MouseEnter">
      <BeginStoryboard Storyboard="{StaticResource MyStoryboard1}">
         <!--Other properties/name if necessary-->
      </BeginStoryboard>
   </EventTrigger>
   <EventTrigger RoutedEvent="Button.MouseDown">
      <BeginStoryboard Storyboard="{StaticResource MyStoryboard1}">
         <!--Other properties/name if necessary-->
      </BeginStoryboard>
   </EventTrigger>
</Style.Triggers>

这个概念是通过共享资源来减少重复代码。

希望这可以帮助!

Sorry... WPF's implementation of the EventTrigger only allows one routed event.

Typically you would be firing a storyboard from a routed event. I would suggest the following compromise:

<!--Define a storyboard as a resource-->
<Storyboard x:Key="MyStoryboard1">
   <!--Many properties and etc...-->
</Storyboard>

<Style.Triggers>
   <EventTrigger RoutedEvent="Button.MouseEnter">
      <BeginStoryboard Storyboard="{StaticResource MyStoryboard1}">
         <!--Other properties/name if necessary-->
      </BeginStoryboard>
   </EventTrigger>
   <EventTrigger RoutedEvent="Button.MouseDown">
      <BeginStoryboard Storyboard="{StaticResource MyStoryboard1}">
         <!--Other properties/name if necessary-->
      </BeginStoryboard>
   </EventTrigger>
</Style.Triggers>

The concept is to reduce duplicate code by sharing resources.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文