开发者

Catching events from nested user control

开发者 https://www.devze.com 2023-02-06 12:44 出处:网络
I have some user controls. The hierarchy is as follows: A contains B B contains C Inside my C control I have a button. When I click on it want my event

I have some user controls. The hierarchy is as follows:

A contains B

B contains C

Inside my C control I have a button. When I click on it want my event

public event EventHandler Print;

to by raised and then I want contro开发者_C百科l A to catch this event. How can I do this?

EDIT 1:

I want to raise my event on User Control C's ViewModel


There are 2 topics that can help you fulfill your need:

Commanding and Routed Events

Both mechanisms are built specifically for wpf's hierarchical structure.

Read up on both, they will help your wpf experience immeasurably.

EDIT: Something more helpful. Put Button.Click="" on the control where you want to catch the event. You can use the button's Tag property to store identifying information so your event handler knows that the correct button was clicked.

<Grid Button.Click="Button_ClickHandler">
    <Button Tag="PrintButton/>
</Grid>

In your code behind for the user control you will require the handler named Button_ClickHandler.

The above method uses Router Events. I really recommend using Commands however.


You can wrap the event like so:

class B
{
    private A a;

    public event EventHandler Print
    {
        add { a.Print += value; }
        remove { a.Print -= value; }
    }
}


I think you want to look at bubbling events.

Update: I didn't see the wpf tag, you should be looking at routed events

0

精彩评论

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