Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

How to: Find the Source Element in an Event Handler

This example shows how to find the source element in an event handler.

Example

The following example shows a Click event handler that is declared in a code-behind file. When a user clicks the button that the handler is attached to, the handler changes a property value. The handler code uses the Source property of the routed event data that is reported in the event arguments to change the Width property value on the Source element.

<Button Click="HandleClick">Button 1</Button>

void HandleClick(object sender, RoutedEventArgs e)
{
    // You must cast the sender object as a Button element, or at least as FrameworkElement, to set Width
    Button srcButton = e.Source as Button;
    srcButton.Width = 200;
}

For the complete sample, see Finding the Source Element in an Event Handler Sample.

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.