# 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.