Solved

Event Handling in c#

Posted on 2014-02-17
5
418 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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