Click here to Skip to main content
13,152,454 members (36,964 online)
Rate this:
Please Sign up or sign in to vote.

I have 2 separate user controls (each one of them on other file):

1. DataGrid

2. Separate User-Control Which contains button.

I'm trying to find a way that when i'm pressing on a button on the 2nd user control, I can catch the event on the DataGrid user-control and to do something with that event (for example, show popup or something like that).

I tried to look for "Raised Event", "Tunneling", "bubbling events" etc. but didn't find a way to do so.

Thanks a lot for the helpers :)
Posted 19-Nov-11 12:44pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Don't try to "find a way" by just "looking" at the topics you have mentioned — first actually read them without connection to your particular problem to understand how events work and then how do they work in WPF. Right now you are so confused that you cannot even formulate what do you want correctly.

The whole idea of "catch the event on the… control…" is wrong because there is no such thing. The even is not "caught" and not even handled on any particular control, but it is fired by some code of some particular class. It is handled by any event handler which you can add to an invocation list of any event instance; and this event handler is not related to any particular control and can be a method of any class or (better) an anonymous method added in the code of any class.

Next thing: you cannot fire an event like Button.Click. This event can be fired and actually already coded to fire in the class Button. You do not need to fire it and, in fact, you never can. An event can only be fired in the class where it is declared; this is one of the important fool-proof limitations of events compared to regular delegate instances.

All you can do with an already defined event is adding some event handler to its invocation list or removing a previously added handler:

class AnyClassAnyAtAll {
    void AnyMethod() {
        myButton.Click += (sender, eventArgs) => {
            //you can do this, but for a button there is nothing specific to know about it:
            Button instance = (Button)sender; //will be myButton, but without using external variable
            //do something essential, for example:
    void DoSomethingOnButtonClick() {/*...*/}

If this is still not clear, you should do the following: 1) set your WPF application aside for a while, as you are not ready yet; 2) read some C# manual (not WPF) from the beginning to the end; 3) make sure you can solve all or most of the simple exercises from the manual, prefer console applications; 4) come back to more complex topics like WPF.

Good luck,
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Here is how to fire an event on another 'thing'.  in this case its a usercontrol.
selectedCtrl.RaiseEvent(new RoutedEventArgs(UserControl.LoadedEvent, selectedCtrl));
I am firing the load event of an object of UserControl.  the specific object is selectCtrl.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170924.2 | Last Updated 30 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100