Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi there
I am using a WPF c# application. I have a user control which contains a datagrid.
I am trying to send a public event from the user control , on a right mouse click.
Here is the created public event
C#
public event MouseButtonEventHandler datagridMouseClick;


Next its supposed to be fired on this event handler of the datagrid:
C#
private void dataGrid1_MouseDown(object sender, MouseButtonEventArgs e)
{

    DependencyObject dep = (DependencyObject)e.OriginalSource;
    while ((dep != null) &&
    !(dep is DataGridCell) &&
    !(dep is DataGridColumnHeader))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }
    if (dep is DataGridCell)
    {
        cell = dep as DataGridCell;
        while ((dep != null) && !(dep is DataGridRow))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }
        row = dep as DataGridRow;

    }
  
        this.datagridMouseClick(sender, e);
 
}

OKK so this above is a code existing at an external object that i made, which has a datagrid, I will load this User Control into another project WPF and it will be named "search", the point of what i am doing here is to fire the PUBLIC event "datagridMouseClick" inside the eventhandler datagrid1_MouseDown (which is the handler of datagrid mouse click event) . Now i Fire the public event "datagridMouseClick" at this point in order to be able to listen to this event from the other project (which will contain this object "search") so that i will be able to handle the datagrid MouseClick event from there (not inside this object but from the other one).
The Other class initializes
C#
public Window1(){
InitializeComponent();//Object search is initialized here

            search.datagridMouseClick += new MouseButtonEventHandler(search_datagridMouseClick); /*this is the event i am tryin to listen to */

            search.datagridDoubleClick +=new RoutedEventHandler(search_datagridDoubleClick);/* this event works correctly eventhough i used the same method as the one above */
}


Ok so what i dont understand here is that theoratically datagridMouseClick should NEVER be null , since a handler is subscribed to it at this point of initialization before ANY possible click (which d be the only way to fire that event).
So,, the compiler never enters inside
search_datagridMouseClick
because the
datagridMouseClick
is null when fired,
I hope i was somewhat clear, i know it is something simple that i am missing. But i could use the help of someone more experienced here. Thank you for your patience, and feel free to ask if i was not clear.
Posted
Updated 14-Nov-11 10:11am
v2
Comments
Sergey Alexandrovich Kryukov 14-Nov-11 15:25pm    
OK, the answers you received so far is no more by guesswork. And this is because you did not do you part. Run into this exception under debugger or catch all exceptions and provide exception information dump. The Exception.Stack will show you the offending line numbers, indicate them.
--SA
Elman90 14-Nov-11 15:53pm    
I didnt put too much info thinking was just something easy that i was missing. will update the question to be a little more clear
Mark Salsbery 14-Nov-11 16:12pm    
Sure it's something easy you're missing! :) Debug it.

Add null check before trying to raise the event as described in other posts.

Use a breakpoint to make sure this code is being called

search.datagridMouseClick += new MouseButtonEventHandler(search_datagridMouseClick);

Put breakpoint at the top of your dataGrid1_MouseDown handler. If you can't then you shouldn't have been able to compile the line of code above that adds the handler. Does the breakpoint get hit ever? If not then that's a problem. If so, is the event you're about to raise still null? If so, then you didn't add a handler to the right event object.
Elman90 14-Nov-11 16:20pm    
Yes i have tried that. The breakpoint hits inside the (search_datagridMouseClick) actually that code works ok till it gets to fire the event. Actually the exception gets generated inside that. When it arrives to this.datagridMouseClick(sender, e);
Generates a NullReferenceException, I have also tried deleting all tho other code except this line and gives the same error. (But the event should be already be subscribed by the search.datagridMouseClick += new MouseButtonEventHandler(search_datagridMouseClick) at this point)
Mark Salsbery 14-Nov-11 16:28pm    
"the event should be already be subscribed by the search.datagridMouseClick += new MouseButtonEventHandler(search_datagridMouseClick) at this point)"

"Should be"? You've confirmed with a breakpoint on the line that subscribes to the event (adds the handler)? If the event is null then it's not subscribed to, and it's good practice to always check before trying to raise the event.

The line
if (dep is DataGridCell)
doesn't check for null values
 
Share this answer
 
Comments
Elman90 14-Nov-11 6:47am    
Yes u r right on that, but its not the point. The event doesnt use dep to be fired, i have even tried to cancel all the code above and leave only "this.datagridMouseClick(sender,e)" and still gives me the exception
At a guess, your datagridMouseClick delegate is not set - pout a breakpoint on the first line and single step though to check.
 
Share this answer
 
Comments
Elman90 14-Nov-11 6:59am    
hello i build this code in an external dll, and i am debugging another application, so cant use breakpoints on this (as far as i know).
OriginalGriff 14-Nov-11 7:19am    
Then you need to check the delegate is set first:
if (this.datagridMouseClick != null)
{
...
Elman90 14-Nov-11 7:26am    
At the breakpoint it says its value is null and type System.Input...MouseButtonEventHandler, nothing else
OriginalGriff 14-Nov-11 7:36am    
Then you have to check for a null handler - if it is null and you try to execute, you will get an exception. And quite rightly! I would strongly recommend checking, rather than the other option of catch-and-ignore the exception.
Elman90 14-Nov-11 8:12am    
I know that, it was originally on the code and i took it off. But thats not the point! By using it that way the event would never be fired, cause its always started at null, i need smthing to initialize the event...
It means that no other class has registered to receive the event. You should always check first whether there are any other classes that have registered to receive the event, like this:-

C#
if (this.datagridMouseClick != null)
            {
                this.datagridMouseClick(sender, e);
            }


hope this helps
 
Share this answer
 
Comments
Elman90 14-Nov-11 6:54am    
Yes i have tried that but that way the event is not fired, since the event is started at null.. I have used the same identical method with a public RoutedEventHandler and worked perfectly, so dont quite get what is the prob here
Wayne Gaylard 14-Nov-11 8:02am    
It is no use firing events if there is no one listening for them.
lukeer 14-Nov-11 8:10am    
Not firing is exactly what the event should do as long as no other software component subscribed to it.

Make sure anyone subscribed to the event and test again.
Elman90 14-Nov-11 8:13am    
I dont quite understand
Elman90 14-Nov-11 8:15am    
and the event WILL be listened in another class, i cant post the ENTIRE code here, , its smth like 10 k lines,, but trust in the other class i take that public event from my usercontrol and assign it to another function, the only prob here is that it is null, i have used this method before and HAS worked perfectly. Can ANYONE help or have any idea on that?
Guys resolved, thanks for your time.
The eventhandler part was right. The point that i realized is that the UIElement part that i was clicking was outside Window1, therefore causing the event be null for that element,eventhough i handle it at Window1. I could not post the entire code here cause its too big.
Thks again,
Best Regards
 
Share this answer
 

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