Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi
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

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:

C#
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:
            DoSomethingOnButtonClick();
        }
    }

    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,
—SA
 
Share this answer
 
v3
SQL
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900