Solved

Event Handling in c#

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now