Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>MouseEventArgs</code> has a <code>OriginalSource</code> property. Its this property which holds a reference to the object that originally triggered it.</p> <p>The <code>sender</code> 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:-</p> <pre><code>&lt;UserControl x:Class="SilverlightApplication1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" &gt; &lt;Grid x:Name="LayoutRoot" Background="White" MouseLeftButtonDown="MouseHandler"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;StackPanel x:Name="OuterPanel" MouseLeftButtonDown="MouseHandler" Margin="5"&gt; &lt;StackPanel x:Name="TopPanel" MouseLeftButtonDown="MouseHandler"&gt; &lt;TextBlock Text="First Top Item" /&gt; &lt;TextBlock Text="Second Top Item" /&gt; &lt;/StackPanel&gt; &lt;StackPanel x:Name="BottomPanel" MouseLeftButtonDown="MouseHandler"&gt; &lt;TextBlock Text="First Bottom Item" /&gt; &lt;TextBlock Text="Second Bottom Item" /&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;ListBox x:Name="lstOutput" Grid.Column="1" Margin="5" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>And in MainPage.xaml.cs add this code:-</p> <pre><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)); } </code></pre> <p>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:-</p> <p><code>Sender: TopPanel, Text block: First Top Item</code><br> <code>Sender: OuterPanel, Text block: First Top Item</code><br> <code>Sender: LayoutRoute, Text block: First Top Item</code> </p> <p>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.</p> <p>Click on the area below the Stack panels. You only get:-</p> <p><code>Sender: LayoutRoot, Text block: Not from a text block</code> </p> <p>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 <code>Handled</code> property to <code>True</code> preventing further bubbling.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload