F # event handler using XAML markup

Now that I have a custom routed event , how can I specify a handler in XAML?

<Window.Resources>
    <Style TargetType="Grid">
        <Setter Property="funk:Tap.Handler"
                Value="{Binding TapHandler}"/>
    </Style>
</Window.Resources>

Resolution:

  • UIElements for handling bubbling or tunneling of RoutedEvents, not just controls that enhance them.
  • Using implicit styles, eliminating the need to hook events for each UIElement of a particular type
  • Changing a logic-based handler in a ViewModel
  • a View without code
+2
source share
1 answer

Using an attached property (based on this post )

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)

+2

Source: https://habr.com/ru/post/1653860/


All Articles