Solved

Event Handling in c#

Posted on 2014-02-17
5
414 Views
Last Modified: 2014-02-19
I have a silverlight .net application.

It is using System.Windows.Controls.DataGrid  ..
Now I want to override SelectionChanged Event of that datagrid...

So in the code i wrote -
mydatagrid.SelectionChanged += new SelectionChangedEventHandler(mydg_SelectionChanged);

Open in new window

             
Now I also want to call the default(original/ base ) callback of that event-SelectionChanged in my callback like this
 void mydg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
              // run Original callback of System.Windows.Controls.DataGrid's SelectionChanged event  ....
             //run my code  
          }

Open in new window

How do i do that

I do not want to create a new DataGrid Class which inherits from System.Windows.Controls.DataGrid and then override SelectionChanged Event callback...
 
please help

thanks a lot
0
Comment
Question by:ts84zs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39867457
There is no "original" callback per se. There may be separate event handler wired up, but these are multicast delegates so many event handlers can be wired up to the same event and they will all fire. In the case of the event you've selected there is a base event handler called OnSelectionChanged. The only reason you would want to override that is if you didn't want the base to run before you code. In that case the correct way to do this would be to override the base implementation of the event handler rather than create your own.

protected override void OnSelectionChanged( SelectionChangedEventArgs e )
{
    // Your code goes here...

    base.OnSelectionChanged( e );
}

Open in new window


If you want the "base implementation", which I put in quotes because that's not entirely accurate, it's a different implementation) to run first you don't need to do anything, as the simple console application below illustrates. In this code the application writes out:

In A Foo Handler
In Main Foo Handler

using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main( string[] args )
        {
            A a = new A();
            a.Foo += a_Foo;

            a.FireFoo();

            Console.ReadKey();
        }

        static void a_Foo( object sender, EventArgs e )
        {
            Console.WriteLine( "In Main Foo Handler" );
        }
    }

    public class A
    {
        public event EventHandler<EventArgs> Foo;

        public A()
        {
            Foo += OnFoo;
        }

        public void FireFoo()
        {
            if( Foo != null )
            {
                Foo( this, EventArgs.Empty );
            }
        }

        private void OnFoo( object sender, EventArgs e )
        {
            Console.WriteLine( "In A Foo Handler" );
        }
    }
}

Open in new window

0
 

Author Comment

by:ts84zs
ID: 39867544
So do i have to create a class which inherits from class System.Windows.Controls.DataGrid  
and then override  OnSelectionChanged callback?

Please help
0
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39867932
So do i have to create a class which inherits from class System.Windows.Controls.DataGrid  
and then override  OnSelectionChanged callback?

Yes. However, as I stated, if you just want the DataGrid's OnSelectionChanged to run before the code in your event handler, that already happens by default. It's only if you want to change that order that you'd need to do anything differently.
0
 

Author Comment

by:ts84zs
ID: 39868579
thanks so much i got the answer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question