XAML Behaviors for Avalonia UI

repository·master·Indexed 43 days ago

https://github.com/wieslawsoltes/xaml.behaviors

A port of the Windows UWP XAML Behaviors framework for Avalonia UI. It enables reusable interactivity, animations, and triggers through reflection-based behaviors and AOT-friendly source generators. The ecosystem includes specialized packages such as Xaml.Behaviors.Animations for standalone composition effects and key-frame animations, as well as interaction libraries for drag-and-drop, ReactiveUI integration, and responsive layouts.

Tokens
185K
Snippets
575
Records
914
Agent score
54%

What's inside XAML Behaviors

  1. Overview of Xaml.Behaviors.Interactions.Events

    master

    The Xaml.Behaviors.Interactions.Events package provides strongly-typed Behaviors and Triggers for Avalonia events. It allows you to handle input events (Pointer, Keyboard, Focus, Gestures, and Drag & Drop) directly in XAML.

    Key capabilities include:

    • Strongly Typed Wrappers: Uses dedicated classes (e.g., PointerPressedEventTrigger) instead of string-based event names.
    • Routing Strategies: Supports the RoutingStrategies property to control whether an event is handled during the Tunnel, Bubble, or Direct phase.
    • Base Classes: Provides abstract *EventBehavior classes to simplify creating custom reusable logic without manual event subscription boilerplate.
  2. Use InputElement interactions

    master

    The InputElement category provides a collection of triggers and actions designed to respond to user input events on UI elements. These allow you to map low-level input events (like pointer movement, key presses, or focus changes) to high-level behaviors or commands.

    Available Triggers

    Triggers detect an event and fire an associated action:

    • Pointer Events: PointerPressedTrigger, PointerReleasedTrigger, PointerMovedTrigger, PointerEnteredTrigger, PointerExitedTrigger, PointerWheelChangedTrigger, PointerCaptureLostTrigger, TappedTrigger.
    • Keyboard Events: KeyDownTrigger, KeyUpTrigger, KeyTrigger, KeyGestureTrigger.
    • Focus Events: GotFocusTrigger, LostFocusTrigger.
    • Text Input: TextInputTrigger, TextInputMethodClientRequestedTrigger.
    • Other: DoubleTappedTrigger, HoldingTrigger.

    Available Actions

    Actions perform a specific operation when a trigger is activated:

    • CapturePointerAction: Captures the pointer to an element.
    • ReleasePointerCaptureAction: Releases the pointer capture.

    Advanced Click Emulation

    The ClickEventTrigger is a specialized tool for complex interaction scenarios. It supports:

    • Click Emulation: Simulating click logic.
    • SourceControl: Managing the source of the event.
    • Event Routing: Configuring HandleEvent or HandledEventsToo strategies to control how events propagate through the visual tree.
    • MVVM Integration: Wiring picker actions directly to MVVM commands.
  3. Use SelectingItemsControl behaviors from Avalonia.Xaml.Interactions.Custom

    master

    The Avalonia.Xaml.Interactions.Custom package provides specialized behaviors designed to work with SelectingItemsControl in Avalonia. These behaviors allow you to extend the functionality of controls like ListBox, ComboBox, or DataGrid by reacting to selection changes or implementing search/filtering logic.

    Available behaviors include:

    • SelectingItemsControlEventsBehavior: An abstract base class used to implement custom logic that responds to the SelectingItemsControl.SelectionChanged event.
    • SelectingItemsControlSearchBehavior: A behavior that enables filtering the items within a SelectingItemsControl based on text input from a search box.
  4. Available Draggable Behaviors

    master

    The package provides several specialized behaviors for different UI interaction scenarios:

    • CanvasDragBehavior: Enables dragging child controls specifically within a Canvas container.
    • MouseDragElementBehavior: Enables dragging a control by applying a TranslateTransform to it.
    • ListReorderDragBehavior: Enables reordering items within an ItemsControl using drag-and-drop functionality.
    • GridDragBehavior: Enables dragging child controls within a Grid, specifically for swapping rows or columns.
  5. Explore Xaml.Behaviors.Animations features in the sample app

    master

    The Animations sample application provides practical examples of the following feature families:

    • Key-frames: Using the key-frame factory, builder, and both synchronous and asynchronous runners.
    • Fluid Movement: Demonstrating movement with automatic transform preparation.
    • Animation Primitives: Implementation of fade, sliding, scale, and rotation.
    • Animation Catalogs: Usage of attention, entrance, exit, Framer Motion, and special catalogs.
    • UI Feedback: Selection indicator animations.
    • Composition Effects: Orbit, tilt, and parallax effects.
    • Collection Operations: Transition collection mutation and observation operations.
  6. Available Validation Behaviors and Rules

    master

    The Validation category provides a set of specialized behaviors and rules for validating UI controls in Avalonia.

    Validation Behaviors

    Behaviors are attached to specific control types to trigger validation logic based on user interaction:

    • ComboBoxValidationBehavior
    • DatePickerValidationBehavior
    • NumericUpDownValidationBehavior
    • PropertyValidationBehavior
    • SliderValidationBehavior
    • TextBoxValidationBehavior

    Validation Rules

    Rules define the specific logic used to determine if a value is valid. These are typically implemented via the IValidationRule interface:

    • IValidationRule (Base interface)
    • MaxValueValidationRule
    • MinLengthValidationRule
    • MinValueValidationRule
    • NotNullValidationRule
    • RangeValidationRule
    • RegexValidationRule
    • RequiredDateValidationRule
    • RequiredDecimalValidationRule
    • RequiredTextValidationRule
  7. Use Avalonia.Xaml.Interactions.Custom to show controls on events

    master

    The Avalonia.Xaml.Interactions.Custom package provides a set of behaviors designed to trigger the visibility of a control in response to specific user interaction events.

    Available behaviors include:

    • ShowOnDoubleTappedBehavior: Shows a control when a double-tap event occurs.
    • ShowOnKeyDownBehavior: Shows a control when a key-down event occurs.
    • ShowOnTappedBehavior: Shows a control when a tap event occurs.

    All these behaviors inherit from ShowBehaviorBase, which serves as the foundation for control visibility logic triggered by events.

  8. Available Gesture Triggers in XAML Behaviors

    master

    The Gestures category provides a variety of interaction triggers that can be used to respond to user input patterns in Avalonia. These triggers allow you to attach specific actions to complex input events like pinching, scrolling, or touchpad-specific gestures.

    Available triggers include:

    • Tapping & Clicking: TappedGestureTrigger, DoubleTappedGestureTrigger, RightTappedGestureTrigger.
    • Holding: HoldingGestureTrigger.
    • Pinch & Zoom: PinchGestureTrigger, PinchEndedGestureTrigger.
    • Scrolling: ScrollGestureGestureTrigger, ScrollGestureEndedGestureTrigger, ScrollGestureInertiaStartingGestureTrigger.
    • Pulling: PullGestureGestureTrigger, PullGestureEndedGestureTrigger.
    • Touchpad Specific: PointerTouchPadGestureSwipeGestureTrigger, PointerTouchPadGestureRotateGestureTrigger, PointerTouchPadGestureMagnifyGestureTrigger.