#603 – Sender, Source and OriginalSource Example

Event handlers for routed events in WPF will have access to three different user interface elements within the handler:

  • sender – the element that raised the event
  • e.Source – element where the event originated
  • e.OriginalSource – original low-level element where the event originated (e.g. sub-element in a control)

Suppose that we have the following logical tree:

    <StackPanel Orientation="Vertical" UIElement.MouseMove="StackPanel_MouseMove">
        <Button Content="Move Mouse Over Me" MouseMove="Button_MouseMove"/>

When we move the mouse over the text on the Button, the MouseMove event fires, first on the Button itself and then on the StackPanel (because MouseMove is a bubbling event).  Here are the values of the three fields when you handled each of these events:

  • MouseMove fires on the Button
    • senderButton
    • SourceButton
    • OriginalSource = TextBlock (the TextBlock that is a child control of the Button)
  • MouseMove fires on the StackPanel
    • senderStackPanel
    • Source = Button
    • OriginalSource = TextBlock

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

One Response to #603 – Sender, Source and OriginalSource Example

  1. Pingback: Dew Drop – July 16, 2012 (#1,364) | Alvin Ashcraft's Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: