using-wpf-behaviors-triggers

Implements XAML behaviors and triggers using Microsoft.Xaml.Behaviors.Wpf for code-behind-free interactivity.

Updated Nov 22, 2023
One-click install
npx skills add https://github.com/parksanghoon-sys/TestCode --skill using-wpf-behaviors-triggers-parksanghoon-sys
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: using-wpf-behaviors-triggers
Source: https://github.com/parksanghoon-sys/TestCode/tree/main/src/Modbus/wpf-dev-pack/.agents/skills/using-wpf-behaviors-triggers
Command: npx skills add https://github.com/parksanghoon-sys/TestCode --skill using-wpf-behaviors-triggers-parksanghoon-sys

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires Microsoft.Xaml.Behaviors.Wpf.

What problem does it solve? Adding interactivity to WPF XAML often forces developers to write event handlers in code-behind, breaking MVVM separation and making UI logic hard to reuse. This Skill provides patterns for wiring events, data conditions, and reusable behaviors entirely in XAML using the Microsoft.Xaml.Behaviors.Wpf library. ## Core Features & Use Cases - EventTrigger and InvokeCommandAction: Bind UI events like Click or SelectionChanged directly to ViewModel commands, with optional event-args passing. - DataTrigger and ChangePropertyAction: React to bound property values by changing UI properties without code-behind. - Custom Behaviors and TriggerActions: Create reusable Behavior<T> and TriggerAction<T> classes with DependencyProperties, including drag-and-drop and focus behaviors. - Use Case: You need a TextBox that selects all text on focus and a Border that accepts dropped files, both bound to ViewModel commands. This Skill shows the exact XAML and C# behavior classes to implement both without touching code-behind. ## Quick Start Ask the AI to add an EventTrigger with InvokeCommandAction to a WPF Button so its Click event invokes a ViewModel command using Microsoft.Xaml.Behaviors.Wpf.

Frequently Asked Questions about using-wpf-behaviors-triggers

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I bind a WPF event to a ViewModel command without code-behind?▼

Use EventTrigger with InvokeCommandAction from Microsoft.Xaml.Behaviors.Wpf inside Interaction.Triggers. Set the EventName (for example Click) and bind the Command property to your ViewModel command, optionally setting PassEventArgsToCommand to True.

How to create a reusable behavior in WPF with Microsoft.Xaml.Behaviors?▼

Create a class inheriting Behavior<T> where T is the target control type, override OnAttached to subscribe to events and OnDetaching to unsubscribe. Attach it in XAML inside Interaction.Behaviors and expose settings via DependencyProperties.

Does Microsoft.Xaml.Behaviors.Wpf require a NuGet package?▼

Yes, you must add the Microsoft.Xaml.Behaviors.Wpf PackageReference (version 1.1.*) to your project. Then declare the namespace xmlns:b="http://schemas.microsoft.com/xaml/behaviors" in your XAML to use Interaction.Triggers and Interaction.Behaviors.

Why does my WPF behavior cause memory leaks?▼

Memory leaks occur when event handlers subscribed in OnAttached are not removed. Always override OnDetaching and unsubscribe every event handler registered on AssociatedObject so the behavior releases its references when detached.

When should I use DataTrigger instead of EventTrigger in WPF behaviors?▼

Use DataTrigger when reacting to bound data values, such as changing Foreground when IsLoading is True. Use EventTrigger for UI events like Click or SelectionChanged that should invoke commands or actions.