Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6811469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:19:53+00:00 2026-05-26T20:19:53+00:00

I am using a WPF c# application. I have a user control which contains

  • 0

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

public event MouseButtonEventHandler datagridMouseClick;

Next its supposed to be fired on this event handler of the datagrid:

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);  // GIVING ERROR
}

It gives me a NullReferenceException. Can u help me figure out why. Thanks in advance, any help appreciated
regards

The event is handled in another class (another project actually since the above is a dll apart) ..
So its listened when the other class is initialized here..

public Window1()
    {
       InitializeComponent();      
        search.datagridDoubleClick +=new RoutedEventHandler(search_datagridDoubleClick);
        search.datagridMouseClick += new MouseButtonEventHandler(search_datagridMouseClick); /* Only this one gives error , even if the other one is handled exactly the same way in the code o.O */

    }

Where search is the name of the object that contains the above 1st code

I think the problem here is that i am trying to listen to an event fired from another class/project (since the first code is from a .dll) that way the current class doesnt initialize a listener and keeps it to null. eventhough i have used this EXACT method on search.datagridDoubleClick above and works perfectly (thats weird).
PS. i dont get the -1 , sounds like a valuable question to me, anyways…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:19:54+00:00Added an answer on May 26, 2026 at 8:19 pm

    You need to check if somebody subscribed to your event by checking if datagridMouseClick is different from Null.

    if (datagridMouseClick != null)
        this.datagridMouseClick(sender, e);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple WPF application in which I am using data binding
I have an application which I am developing using WPF\Prism\MVVM. All is going well
I have a WPF application using MVVM. I have some user controls that show
I have a WPF application where I'm using dependency properties in codebehind which I
I am developing a WPF application, and I have created a user control called
I have a WPF user control for which I need to force rendering in
I'm developing a WPF application using MVVM. Inside a Window, I have a control
In my WPF application I have some drawing functionality. I have solved this using
I have a WPF application using Aero Glass. When using the application under a
I have a WPF Application using Fluent NHibernate 1.0 RTM and System.Data.SQLite 1.0.65 that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.