$ show wieslawsoltes/Xaml.Behaviors
This repository is a comprehensive XAML Behaviors library for Avalonia UI, porting the Microsoft.Xaml.Behaviors pattern to the Avalonia framework. It provides a declarative, XAML-friendly way to attach interactive logic (behaviors, triggers, actions) to UI controls without code-behind, covering common patterns such as event handling, data-driven triggers, command invocation, property changes, drag-and-drop, animations, validation, focus management, and more. The library is split into multiple NuGet packages: core interactivity (base classes), interactions (ready-to-use triggers/actions), custom interactions (control-specific behaviors), drag-and-drop, draggable elements, events, animations, responsive layout, scripting, and ReactiveUI integration. A key innovation is the use of Roslyn source generators to produce strongly-typed, reflection-free versions of actions and triggers, improving AOT compatibility and compile-time safety. The architecture follows a layered design: the foundation is the `Avalonia.Xaml.Interactivity` namespace with `Behavior`, `Trigger`, `Action` base classes and the `Interaction` static class for attaching behaviors. On top of that, `Avalonia.Xaml.Interactions.Core` provides common triggers/actions like `EventTriggerBehavior`, `InvokeCommandAction`, `ChangePropertyAction`, and `DataTriggerBehavior`. The `Custom` package adds hundreds of specialized behaviors for controls (e.g., `AutoCompleteBoxOpenDropDownOnFocusBehavior`, `SplitViewStateBehavior`, `FocusTrapBehavior`). Drag-and-drop is handled by a dedicated framework with `DropHandlerBase`, `ManagedDragDropService`, and behaviors like `PanelDragBehavior`. Animations are built using Avalonia's composition API, with helpers like `AnimationFactory` and `FluidMoveAnimation`. The source generators in `Xaml.Behaviors.SourceGenerators` produce typed wrappers from attributes like `[GenerateTypedAction]` and `[GenerateTypedTrigger]`, reducing boilerplate and runtime reflection. Key entry points for consumers are the `Interaction.Behaviors` attached property in XAML, the `Interaction.ExecuteActions` method, and the various behavior/trigger/action classes. The library depends on Avalonia UI (StyledElement, Control, Visual, TopLevel, etc.) and optionally on ReactiveUI. It includes extensive documentation via DocFX, sample applications demonstrating all features, and unit tests using Avalonia's headless testing infrastructure. The codebase is well-structured with clean abstractions, though its large surface area and reliance on internal Avalonia APIs present some maintenance risks.
The LLM identified 5 files worth highlighting for reuse:
Provides a reusable observer that subscribes to ICommand.CanExecuteChanged using weak references and exposes a boolean property. Used by InvokeCommandAction and InvokeCommandBehaviorBase to enable/disable controls based on command state. Demonstrates a clean pattern for bridging ICommand lifecycle to UI without memory leaks.
A generic class that tracks and reverts property changes on AvaloniaObject instances. Supports stack-based undo and is used by ChangePropertyAction and generated typed actions. Demonstrates a robust pattern for reversible UI mutations, applicable to any property change scenario.
Abstract base class for drag-and-drop handlers with Validate and Execute methods. Provides a clean separation of concerns for drop targets, supporting both managed and OS-level drag operations. Reusable for implementing custom drop logic in any Avalonia application.
Static factory class that creates predefined animations (fade, scale, rotate, slide, etc.) using Avalonia's composition API. Returns Animation instances that can be applied to any visual element. Demonstrates a clean, reusable pattern for building and composing animations without coupling to the behaviors framework.
The main Roslyn incremental generator that produces typed actions, triggers, property triggers, event commands, and async/observable triggers from attributes. Uses a well-structured pipeline with attribute collection, deduplication, and source emission. Demonstrates advanced source generator patterns including assembly-level attribute support, diagnostics, and accessibility checks.