For example something like that:
Public Partial Class MyWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Protected Overrides Sub OnContentRendered(e As EventArgs)
MyBase.OnContentRendered(e)
Dim myButton As New Button()
myButton.Content = "_My Button"
myGrid.Children.Add(myButton)
myButton.Click += Sub(sender As Object, eventArgs As RoutedEventArgs)
AddHandler myButton.Click, Sub(sender As Object, eventArgs As RoutedEventArgs)
End Sub
End Sub
End Class
Of course, you can add a button on the event you want, even when the window is already showing.
I demonstrated the most convenient way to add a handler to the invocation list of a routed event instance, with anonymous delegate. Not only you won't need to write method which you don't use otherwise, but you can use handling method with the signature convenient for you, without passing both
sender
and
eventArgs
parameters if you don't need them.
—SA