WPF 如何创建自定义tunelroutedevent?

发布于 2024-08-02 15:38:15 字数 369 浏览 7 评论 0原文

我需要一个从父控件到子控件的隧道路由事件隧道的简单示例。

(这不是真实代码)——事实上,我越深入,我就越认为 XAML 是错误的——可能不应该在子节点上的 XAML 中注册隧道事件(不确定?)

<PARENT>    
   <MyControl DoSomethingOnUserAction="raiseTunnelEvent"> HELP </MyControl >    
   <CHILD> I SHOULD HANDLE tunnelled event </CHILD> 
</PARENT>

简单、简洁的例子会很有帮助。

谢谢, 艾伦

I need a simple example of a Tunnel Routed Event tunnelling from a parent control to a child control.

(THIS IS NOT REAL CODE) -- in fact, the deeper I go, the more I think that the XAML is wrong -- probably should NOT sign up for the tunnelled event in XAML on the child node (not sure?)

<PARENT>    
   <MyControl DoSomethingOnUserAction="raiseTunnelEvent"> HELP </MyControl >    
   <CHILD> I SHOULD HANDLE tunnelled event </CHILD> 
</PARENT>

Simple, concise example would be helpful.

Thanks,
Alan

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

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

发布评论

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

评论(2

风和你 2024-08-09 15:38:15

不确定,但你可能想要一只猫吠叫。

Button 的 RoutedEvent ClickEvent(来自PresentationFramework)声明为:

public static readonly RoutedEvent ClickEvent = 
    EventManager.RegisterRoutedEvent("Click", 
    RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof
    (ButtonBase));

请注意Bubblereadonly RoutingStrategy

以下内容可能有助于理解隧道、气泡和直接:
msdn.microsoft.com/en-us/library/system.windows.routingstrategy.aspx

这应该会引导您完成剩下的工作:
msdn.microsoft.com/en-us/magazine/cc785480.aspx

提示:按照惯例,WPF 中的隧道事件以“Preview”开头(例如“PreviewExplode”)。如果事件不以“Preview”开头,则可能不会不要使用隧道 RoutingStrategy。此外,您通常会看到隧道和气泡配对,隧道首先触发,然后是气泡,如“PreviewExplode”,然后是“Explode”。

如果您需要按钮的单击隧道,您可以考虑

  1. 使用 。 PreviewMouseDown(当然不一样,并且可能很危险,因为并非所有鼠标按下都意味着单击)。
  2. 编写一个引发 PreviewClick 和 Click 的 TunnelButton。

Not sure, but you may be wanting a cat to bark.

The RoutedEvent ClickEvent of Button (from PresentationFramework) is declared as:

public static readonly RoutedEvent ClickEvent = 
    EventManager.RegisterRoutedEvent("Click", 
    RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof
    (ButtonBase));

Note the readonly RoutingStrategy of Bubble.

The following may help with understanding Tunnel, Bubble, and Direct:
msdn.microsoft.com/en-us/library/system.windows.routingstrategy.aspx

And this should take you the rest of the way:
msdn.microsoft.com/en-us/magazine/cc785480.aspx

A tip: by convention tunneling events in WPF begin with "Preview" (e.g.- "PreviewExplode". If the event doesn't begin with "Preview" it probably doesn't use the tunnel RoutingStrategy. Also you will usually see a Tunnel and Bubble paired with the Tunnel firing first then the Bubble as in "PreviewExplode" followed by "Explode".

If you need to have a Button's Click tunnel, you might consider

  1. using PreviewMouseDown (not the same of course and likely dangerous since not all mouse-downs are meant to become clicks).
  2. Writing a TunnelButton that raises a PreviewClick and then a Click.
ゞ记忆︶ㄣ 2024-08-09 15:38:15

事实证明,《I have WPF In Action with Visual Studio 2008》一书(第 149 页)有一个非常具有误导性的图表,该图表似乎表明可以在父节点中引发 RoutedEvents,并且路由事件通过隧道传输到子节点。

事实证明并非如此... 1/2 天的糟糕图表被吹坏了(否则相当不错的书)[ ISBN : 1-933988-22-3 ]

Turns out the book I have WPF In Action with Visual Studio 2008, on page 149, has a very misleading diagram that seems to indicate that RoutedEvents can be raised in a parent node and that routed event tunnelled to child nodes.

That turns out to NOT be the case... 1/2 day blown b/c of bad diagram (fairly good book otherwise) [ ISBN : 1-933988-22-3 ]

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