Solved

Event Handling in c#

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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