Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 605153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:06:55+00:00 2026-05-13T17:06:55+00:00

I recently created a Silverlight 3 app in which I created some UI elements

  • 0

I recently created a Silverlight 3 app in which I created some UI elements in the code behind and added them at run-time dynamically.

I was hoping to just use the built-in MouseButtonEventArgs or the sender object to get a reference to the instance that was clicked, however I noticed once I started that this was not the case. I was not able to access any properties of the object that triggered the event and program against it.

 void myFunc(object sender, MouseButtonEventArgs e)
    {
        //Can't do this :(           
        sender.someProperty = someValueToUpdate;
        //or this
        MyClass foo = sender as MyClass;
        foo.someProperty = someValueToUpdate;

    }

I ended up just writing a CustomEventArgs object to pass an instance, but it surprised me that this wasn’t a default behavior.

Can anyone shed some light as to WHY the sender object doesn’t contain a reference to the object that triggered the event?

Also, here is what I did to get that instance.

myObject.myEvent += new CustomEvent(myFunc);        
...
void myFunc(object sender, CustomEventArgs e)
            {
                 e.MyProperty = someValueToUpdate;
            }
...
     public class MyClass 
         {
            public MyProperty = 0;    
            public event CustomEvent myEvent;
            protected virtual void MyEventMethod(CustomEventArgs e)
            {
                if (myEvent != null){myEvent(this, e);}
            }  
            public MyClass ()
            {
             this.MouseLeftButtonDown += new MouseButtonEventHandler(this_MouseLeftButtonDown);
            }
            void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                CustomEventArgs e2 = new CustomEventArgs(this);
                MyEventMethod(e2);
            }
        }   

     public class CustomEventArgs : EventArgs
        {
            private readonly MyClass myProperty;
            public CustomEventArgs(MyClass myProperty) { this.myProperty = myProperty; }
            public MyClass MyProperty { get { return myProperty; } }
        }
     public delegate void CustomEvent(object sender, CustomEventArgs e);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:06:55+00:00Added an answer on May 13, 2026 at 5:06 pm

    The MouseEventArgs has a OriginalSource property. Its this property which holds a reference to the object that originally triggered it.

    The sender argument quite rightly is set to the instance of the object against which you attached the event handler. Perhaps a simple experiment will make how this hangs together clearer. In Visual Studio create a Silverlight Application. Make the content of the MainPage.xaml look like this:-

    <UserControl x:Class="SilverlightApplication1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       >
        <Grid x:Name="LayoutRoot" Background="White" MouseLeftButtonDown="MouseHandler">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <StackPanel x:Name="OuterPanel" MouseLeftButtonDown="MouseHandler" Margin="5">
                <StackPanel x:Name="TopPanel" MouseLeftButtonDown="MouseHandler">
                    <TextBlock Text="First Top Item" />
                    <TextBlock Text="Second Top Item" />
                </StackPanel>
                <StackPanel x:Name="BottomPanel" MouseLeftButtonDown="MouseHandler">
                    <TextBlock Text="First Bottom Item" />
                    <TextBlock Text="Second Bottom Item" />
                </StackPanel>
            </StackPanel>
            <ListBox x:Name="lstOutput" Grid.Column="1" Margin="5" />
        </Grid>
    </UserControl>
    

    And in MainPage.xaml.cs add this code:-

        private void MouseHandler(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement s = sender as FrameworkElement;
            TextBlock o = e.OriginalSource as TextBlock;
            string text = (o != null) ? o.Text : "Not from a text block";
            lstOutput.Items.Add(String.Format("Sender: {0}, Text block: {1}", s.Name, text));
        }
    

    Note how this same handler is attached to three different items in the XAML but not to the TextBlocks themselves. Clicking the “First Top Item” gets you this:-

    Sender: TopPanel, Text block: First Top Item
    Sender: OuterPanel, Text block: First Top Item
    Sender: LayoutRoute, Text block: First Top Item

    The handler fires 3 times once for each item it is attached to as can be seen by the sender being different for each one. However the OrignalSource it the TextBlock that was actually clicked on despite it not having any handler attached. Also note that the OriginalSource remains the same as it bubbles up the ancestor elements.

    Click on the area below the Stack panels. You only get:-

    Sender: LayoutRoot, Text block: Not from a text block

    Of interest also is that clicking in the Listbox results in no items being added at all, you might expect to the same ase the above line. Clearly ListBox handles the mouse down and therefore sets the event args Handled property to True preventing further bubbling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 440k
  • Answers 440k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer check your spelling var linkElementLnk = document.getElementById("BackButtonlnk"); ... linkElementLink.style.display =… May 15, 2026 at 5:21 pm
  • Editorial Team
    Editorial Team added an answer http://msdn.microsoft.com/en-us/library/system.io.driveinfo.totalfreespace.aspx Copied from the link using System; using System.IO; class… May 15, 2026 at 5:21 pm
  • Editorial Team
    Editorial Team added an answer This is a bit simpler then the above, haven't fully… May 15, 2026 at 5:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.