wpf 中的路由事件

发布于 2024-07-17 08:15:51 字数 470 浏览 3 评论 0原文

一个众所周知的事实是,路由事件仅沿着祖先向上或向下传播,而不是穿过所有元素。

但同样众所周知的事实是,事件处理程序可以连接到两个不同控件的公共父级中。

我的问题是如何,请指点。

我的 XAML 看起来像

<Grid Name="MainGrid">
    <Listbox Name="lb1"/>
    <Grid Name="InnerGrid">
       <Listbox Name="lb2"/>
    </Grid>
</Grid>

“lb1”上定义的鼠标事件处理程序不会被触发,因为它被“埋藏”在“InnerGrid”下。 我的问题是如何在“MainGrid”或其他地方编写一些代码,其中“lb1”上定义的事件处理程序被触发。 或者可能是实现这一目标的其他技术。

非常感谢。

A very well known fact is that routed events only travel up or down the ancestry and not through all the elements.

But an equally known fact is that an event handler can be wired in the common parent of two disparate controls.

My questions is how, some pointers please.

I have XAML that looks like

<Grid Name="MainGrid">
    <Listbox Name="lb1"/>
    <Grid Name="InnerGrid">
       <Listbox Name="lb2"/>
    </Grid>
</Grid>

The Mouse Event handlers defined on "lb1" will not get fired as it gets "burried" under "InnerGrid". My question is how can I wrote some code in the "MainGrid" or somewhere else wherein the event handlers defined on "lb1" get fired. Or may be some other technique of achieveing this.

Many Thanks.

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

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

发布评论

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

评论(1

儭儭莪哋寶赑 2024-07-24 08:15:51

我不确定您到底在问什么,但似乎您希望能够为已经处理的事件接到电话。

试试这个:

InitializeComponent() 之后的构造函数后面的代码中,

this.AddHandler(RoutedEvent, Delegate, bool);

在事件中调用 Pass(MouseDown 等),要调用的委托(类似于 lb1_MouseDown),然后 true 表示您希望为已处理的事件调用。

I'm not sure exactly what you are asking, but it seems you want to be able to get called for events that have already been handled.

Try this:

In your code behind in the constructor after InitializeComponent(), call

this.AddHandler(RoutedEvent, Delegate, bool);

Pass in the event (MouseDown or etc), the delegate to call (something like lb1_MouseDown), and then true to indicate that you want to be called for events that have already been handled.

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