Solved

Event Handling in c#

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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…
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.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

18 Experts available now in Live!

Get 1:1 Help Now