The ViewModel layer is contained in the CalcViewModel project and serves as the intermediary between the View (XAML) and Model (CalcManager). ViewModels expose data for UI binding and handle UI-specific logic without coupling to specific UI controls.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/microsoft/calculator/llms.txt
Use this file to discover all available pages before exploring further.
Project Structure
ViewModel Hierarchy
The ViewModel structure mirrors the View hierarchy:ApplicationViewModel
Root ViewModel for
MainPage.xamlManages mode switching and coordinates child ViewModelsStandardCalculatorViewModel
ViewModel for
Calculator.xamlHandles Standard, Scientific, and Programmer modesDateCalculatorViewModel
ViewModel for
DateCalculator.xamlDate calculation logicUnitConverterViewModel
ViewModel for
UnitConverter.xamlAll converter modes including currencyCore Responsibilities
ViewModels in Calculator:PropertyChanged Events
The
INotifyPropertyChanged interface is the foundation of data binding in MVVM. It allows ViewModels to notify the UI when properties change.INotifyPropertyChanged Interface
ViewModels implement this interface to support data binding:StandardCalculatorViewModel.h Declaration
OBSERVABLE_OBJECT Macro
TheOBSERVABLE_OBJECT() macro (defined in Utils.h) implements the required PropertyChanged event:
- Declares the
PropertyChangedevent - Provides a
RaisePropertyChangedhelper function
Observable Properties
Calculator uses macros to simplify property declarations that automatically triggerPropertyChanged events.
OBSERVABLE_PROPERTY_RW
Defines a Read/Write property with automatic change notification:The setter checks if the new value differs from the current value before raising
PropertyChanged, avoiding unnecessary events.OBSERVABLE_PROPERTY_R
Defines a Read-Only property (setter is private):Read-Only Property
Property Examples from StandardCalculatorViewModel
Data Binding Flow
Here’s how property changes flow to the UI:Example: Display Value Update
- ViewModel Property
- XAML Binding
- Update Flow
StandardCalculatorViewModel.h
Commands
ViewModels expose commands for user actions using theCOMMAND_FOR_METHOD macro.
Command Declaration
COMMAND_FOR_METHOD Macro
Command Binding in XAML
MainPage.xaml Command Binding
OnCopyCommand in the ViewModel.
ViewModel-Model Interaction
ViewModels call into the Model layer throughCalculatorManager:
ViewModel → Model Flow
The Model communicates back to the ViewModel through the
ICalcDisplay interface, which the ViewModel implements.Complex Property Example
Some properties have custom logic beyond simple get/set:StandardCalculatorViewModel.h Custom Property (src/CalcViewModel/StandardCalculatorViewModel.h:98-114)
Best Practices
Use Observable Macros
Use Observable Macros
Use
OBSERVABLE_PROPERTY_RW or OBSERVABLE_PROPERTY_R for properties that trigger UI updates. This ensures consistent behavior and reduces boilerplate.Check Value Changes
Check Value Changes
Always check if the new value differs before raising
PropertyChanged to avoid unnecessary UI updates. The macros handle this automatically.No UI References
No UI References
ViewModels should never reference XAML controls or UI-specific types. Use data binding and commands instead.
Transform for Display
Transform for Display
Convert Model data into display-friendly formats in the ViewModel. For example, format numbers with appropriate precision.
Use Read-Only Properties
Use Read-Only Properties
Prefer
OBSERVABLE_PROPERTY_R for properties that shouldn’t be modified by the View.Property Change Callbacks
UseOBSERVABLE_OBJECT_CALLBACK to react to any property change:
Callback Example
Testing ViewModels
Next Steps
Model Layer
Understand how ViewModels interact with CalcManager
View Layer
See how XAML binds to ViewModel properties
MVVM Pattern
Review the overall architecture pattern