Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 Smile | :)
Posted 19-Nov-11 13:44pm
Rate this: bad
good
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:
            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
  Permalink  
v3
Rate this: bad
good
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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 30 May 2012
Copyright © CodeProject, 1999-2014
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