如何:尋找事件處理常式中的來源項目
本範例會顯示如何尋找事件處理常式中的來源元素。
範例
下列範例會顯示在程式碼後置檔案中宣告的 Click 事件處理常式。 當使用者按一下附加處理常式的按鈕時,該處理常式即會變更屬性值。 處理常式程式碼會使用事件引數中報告的路由事件資料的 Source 屬性來變更 Width 元素的 Source 屬性值。
<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;
}
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