如何在 Silverlight 用户控件中冒泡事件?

发布于 2024-10-21 04:52:57 字数 1038 浏览 3 评论 0原文

我正在尝试将事件从子用户控件冒泡到其父控件。

子用户控件是网格内的按钮:

<UserControl>
    <Grid>
        <Button Click="Button_Click" />
    </Grid>
</UserControl>

父用户控件由子控件的许多实例组成:

<UserControl>
    <StackPanel>
        <customs:myButton CustomClick="something" />
        <customs:myButton CustomClick="something" />
        <customs:myButton CustomClick="something" />
        etc.
    </StackPanel>
</UserControl>

在我定义的子用户控件中:

    public delegate void CustomClickHandler(object sender, EventArgs e);
    public event CustomClickHandler CustomClick;

“内部”按钮以这种方式处理单击事件:

    private void Button_Click(object sender, EventArgs e)
    {
        if (CustomClick != null)
            CustomClick (sender, e);
    }

我尝试过检查发生了什么,我可以看到 Button_Click 被调用,CustomClick 不为​​ null 并且它被执行。然而似乎什么也没有发生,即使在父用户控件中附加的代码也不会被调用。

有什么建议吗?

提前致谢, 干杯, 吉安卢卡.

I am trying to bubble an event from a child usercontrol to its parent.

The child usercontrol is a button inside a grid:

<UserControl>
    <Grid>
        <Button Click="Button_Click" />
    </Grid>
</UserControl>

The parent usercontrol is composed by many instances of the child control:

<UserControl>
    <StackPanel>
        <customs:myButton CustomClick="something" />
        <customs:myButton CustomClick="something" />
        <customs:myButton CustomClick="something" />
        etc.
    </StackPanel>
</UserControl>

In the child usercontrol I have defined:

    public delegate void CustomClickHandler(object sender, EventArgs e);
    public event CustomClickHandler CustomClick;

and the "inner" button handles the click event in this way:

    private void Button_Click(object sender, EventArgs e)
    {
        if (CustomClick != null)
            CustomClick (sender, e);
    }

I've tried to check what it is going on, and I can see the Button_Click is invoked, CustomClick is not null and it gets executed. However nothing seems to happen, the code attached to that even in the parent usercontrol is not called.

Any suggestion?

Thanks in advance,
Cheers,
Gianluca.

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

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

发布评论

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

评论(2

傲鸠 2024-10-28 04:52:57

您正在寻找的称为路由事件。您可以编写自己的自定义库或查看此

What you are seeking is called routed events. You can write your own custom ones or take a look at this library.

阪姬 2024-10-28 04:52:57

好的,我已经解决了这个问题,不过没有什么问题。

第一个问题是由 IsHitTestVisible 引起的。在一些在线文章中据说将该属性设置为 false,以解决一些与鼠标事件相关的问题。我这样做了,但这是错误的,因为该属性设置为 false 的元素正在“捕获”鼠标事件,并且它们不再到达内部用户控件。

其次,在最内部的按钮中(请参阅上面的帖子以了解该场景),为了使其正常工作,我必须设置 ClickMode="Hover" 并处理 MouseLeftButtonUp 事件。我将尝试使用标准点击,但我在某处读到只有某些事件冒泡...

e.Handled 不需要任何更改:我已经检查过,它已经是错误的。

我也不必使用任何 RoutedEvent 库...

我想不出我还做了什么来解决这个问题。

我想这就是全部了。

一如既往,如果您有任何建议,请随时在此处添加。

干杯,
吉安卢卡.

Ok, I have sorted this out, there were few problems though.

The first issue was caused by IsHitTestVisible. In some online articles it was said to set that property to false, in order to resolve some mouse-event related issues. I did so, but that was wrong because the element with that property set to false was "catching" the mouse events, and they were not arriving anymore to the inner usercontrols.

Secondly, in the most internal button (see my post above to understand the scenario), in order to get this to work, I've had to set the ClickMode="Hover" and to handle the MouseLeftButtonUp event. I'll try to use the standard click but I read somewhere that only certain event bubble up...

e.Handled did not need any change: I've checked and it was already false.

Neither I had to use any RoutedEvent library...

I cannot think of anything else I did to resolve the issue.

I guess this is all.

As always, if you have any suggestion, please feel free to add them here.

Cheers,
Gianluca.

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