使用附加属性(基于这篇文章)
type Tap() =
inherit DependencyObject()
// For easy exchange
static let routedEvent = MyButtonSimple.TapEvent
static let HandlerProperty =
DependencyProperty.RegisterAttached
( "Handler", typeof<RoutedEventHandler>,
typeof<Tap>, new PropertyMetadata(null))
static let OnEvent (sender : obj) args =
let control = sender :?> UIElement
let handler = control.GetValue(HandlerProperty) :?> RoutedEventHandler
if not <| ((handler, null) ||> LanguagePrimitives.PhysicalEquality) then
handler.Invoke(sender, args)
static do EventManager.RegisterClassHandler(
typeof<FrameworkElement>, routedEvent,
RoutedEventHandler(OnEvent))
static member GetHandler (element: UIElement) : RoutedEventHandler =
element.GetValue(HandlerProperty) :?> _
static member SetHandler (element: UIElement, value : RoutedEventHandler) =
element.SetValue(HandlerProperty, value)
wpfApp 演示文件可以在这里找到(FsXaml 2.1.0)