Files
Tris ShoresandAndy De George 51977cea61 Content update - How to create a custom routed event (user story 1878475) (#1289)
* Add article, redirects, toc

* Update dotnet-desktop-guide/net/wpf/events/how-to-create-a-custom-routed-event.md

Co-authored-by: Andy (Steve) De George <[email protected]>

* Update dotnet-desktop-guide/net/wpf/events/snippets/how-to-create-a-custom-routed-event/csharp/CodeSample.csproj

Co-authored-by: Andy (Steve) De George <[email protected]>

* Update dotnet-desktop-guide/net/wpf/events/snippets/how-to-create-a-custom-routed-event/vb/CodeSampleVb.vbproj

Co-authored-by: Andy (Steve) De George <[email protected]>

* Update dotnet-desktop-guide/net/wpf/events/snippets/how-to-create-a-custom-routed-event/WpfControlLibraryCsharp/WpfControlLibrary.csproj

Co-authored-by: Andy (Steve) De George <[email protected]>

* Use local project references

Co-authored-by: Andy (Steve) De George <[email protected]>
2022-02-10 14:49:46 -08:00

54 lines
1.7 KiB
VB.net

Imports System.Windows
Imports System.Windows.Controls
'<CustomButton>
Public Class CustomButton
Inherits Button
' Register a custom routed event with the Bubble routing strategy.
Public Shared ReadOnly ConditionalClickEvent As RoutedEvent = EventManager.RegisterRoutedEvent(
name:="ConditionalClick",
routingStrategy:=RoutingStrategy.Bubble,
handlerType:=GetType(RoutedEventHandler),
ownerType:=GetType(CustomButton))
' Provide CLR accessors to support event handler assignment.
Public Custom Event ConditionalClick As RoutedEventHandler
AddHandler(value As RoutedEventHandler)
[AddHandler](ConditionalClickEvent, value)
End AddHandler
RemoveHandler(value As RoutedEventHandler)
[RemoveHandler](ConditionalClickEvent, value)
End RemoveHandler
RaiseEvent(sender As Object, e As RoutedEventArgs)
[RaiseEvent](e)
End RaiseEvent
End Event
Private Sub RaiseCustomRoutedEvent()
' Create a RoutedEventArgs instance.
Dim routedEventArgs As New RoutedEventArgs(routedEvent:=ConditionalClickEvent)
' Raise the event, which will bubble up through the element tree.
[RaiseEvent](routedEventArgs)
End Sub
' For demo purposes, we use the Click event as a trigger.
Protected Overrides Sub OnClick()
' Some condition combined with the Click event will trigger the ConditionalClick event.
If Date.Now > New DateTime() Then RaiseCustomRoutedEvent()
' Call the base class OnClick() method so Click event subscribers are notified.
MyBase.OnClick()
End Sub
End Class
'</CustomButton>