Files
MyAiConfiguration/shared/rules/wpf/references/mvvm-and-binding.md
T
Julian LechnerandClaude Opus 5.5 df09ddeaa9 docs(rules): add WPF accessibility and theming, trim WPF rules
A new WPF topic rule covers accessibility (AutomationProperties names
and help text, keyboard access, visible focus, high contrast through
SystemColors, automation peers for custom controls), DPI scaling in
device-independent units with tests across scale factors and monitors,
and the Fluent theme through ThemeMode on .NET 9 and later.

The existing WPF rules lose about a quarter of their size by merging
one-line generic bullets that the C# and shared rules already cover,
while keeping the WPF-specific pitfalls. Duplicate async void and
binding error guidance is merged, client secret handling defers to the
client app security rule, the architecture rule asks to check .NET
Framework versus .NET before using newer APIs, and the CommunityToolkit
note states that partial properties require C# 14.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:14:24 +01:00

32 lines
2.3 KiB
Markdown

# WPF MVVM, Binding, and Properties
Apply these rules when writing view models, data bindings, property notifications, dependency properties, or behaviors.
## MVVM
* Keep view models to presentation state and commands; keep domain logic in services.
* Never reference `Window`, `UserControl`, `DispatcherObject`, or other visual tree elements from view models.
* Open dialogs, message boxes, and navigation through application services, not directly from view models.
* Keep dependencies minimal, derive state instead of duplicating it, and keep state transitions predictable.
## Data Binding
* Prefer data binding over manual UI synchronization, with strongly typed properties and short, stable binding paths.
* Use `OneWay` unless the UI must modify the source, and avoid `UpdateSourceTrigger=PropertyChanged` when the update is expensive.
* Treat binding errors as defects: check the debugger output or XAML binding failure diagnostics after UI changes, and use `FallbackValue` and `TargetNullValue` only for intended behavior, not to hide broken bindings.
* Expose correctly shaped state from the view model instead of converter or `MultiBinding` logic, and never put business logic in converters.
## Property Change Notifications
* Raise `PropertyChanged` only when a value actually changes, and keep dependent notifications explicit instead of hidden chains.
* Use the project's MVVM library for notifications. Without one, prefer CommunityToolkit.Mvvm source generators (`[ObservableProperty]`, `[RelayCommand]`; on partial properties from 8.4.1, which requires C# 14) or a base class using `[CallerMemberName]`. Avoid reflection- or proxy-based notification frameworks unless the project already uses one.
## Dependency Properties
* Use dependency properties only when the WPF property system is required, not as a replacement for CLR properties.
* Keep metadata and property-changed callbacks lightweight and free of hidden side effects, coerce or validate only when the control contract requires it, and never use mutable default values in metadata.
## Attached Properties and Behaviors
* Use attached properties and behaviors for reusable view-specific behavior, never to hide business logic. Keep them focused, avoid global state in them, and clean up event handlers when they detach.