Compartilhar via


Como: Localizar o elemento fonte em um manipulador de eventos

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

Exemplo

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>
Private Sub HandleClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
    'You must cast the object as a Button element, or at least as FrameworkElement, to set Width
    Dim srcButton As Button
    srcButton = CType(e.Source, Button)
    srcButton.Width = 200
End Sub
        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;
        }

Consulte também

Referência

RoutedEventArgs

Conceitos

Visão geral sobre eventos roteados

Outros recursos

Events How-to Topics