开发者

How to bubble events in Silverlight usercontrols?

I am trying to bubble an event from a child usercontrol t开发者_StackOverflow社区o 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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜