$npx -y skills add managedcode/dotnet-skills --skill maui-data-bindingGuidance for .NET MAUI XAML and C# data bindings — compiled bindings, INotifyPropertyChanged / ObservableObject, value converters, binding modes, multi-binding, relative bindings, fallbacks, and MVVM best practices. USE FOR: setting up compiled bindings with x:DataType, implement
| 1 | # .NET MAUI Data Binding |
| 2 | |
| 3 | Wire UI controls to ViewModel properties with compile-time safety, correct |
| 4 | change notification, and minimal overhead. Prefer compiled bindings everywhere |
| 5 | and treat binding warnings as build errors. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Adding `x:DataType` compiled bindings to a new or existing page |
| 10 | - Implementing `INotifyPropertyChanged` or CommunityToolkit `ObservableObject` |
| 11 | - Creating or consuming `IValueConverter` / `IMultiValueConverter` |
| 12 | - Choosing the correct `BindingMode` for a control property |
| 13 | - Setting `BindingContext` in XAML or code-behind |
| 14 | - Using relative bindings (`Self`, `AncestorType`, `TemplatedParent`) |
| 15 | - Applying `StringFormat`, `FallbackValue`, or `TargetNullValue` |
| 16 | - Writing AOT-safe code bindings with `SetBinding` and lambdas (.NET 9+) |
| 17 | |
| 18 | ## When Not to Use |
| 19 | |
| 20 | - **CollectionView layouts / templates** — use the `maui-collectionview` skill |
| 21 | - **Shell navigation parameters** — use the `maui-shell-navigation` skill |
| 22 | - **Service registration / DI** — use the `maui-dependency-injection` skill |
| 23 | - **Property-change-triggered animations** — use built-in [.NET MAUI animation APIs](https://learn.microsoft.com/dotnet/maui/user-interface/animation/basic) |
| 24 | |
| 25 | ## Inputs |
| 26 | |
| 27 | - A .NET MAUI project targeting .NET 8 or later |
| 28 | - XAML pages or C# code-behind where bindings are declared |
| 29 | - A ViewModel class (or plan to create one) |
| 30 | |
| 31 | --- |
| 32 | |
| 33 | ## Compiled Bindings — x:DataType Placement |
| 34 | |
| 35 | Compiled bindings are **8–20× faster** than reflection-based bindings and are |
| 36 | required for NativeAOT / trimming. Enable them with `x:DataType`. |
| 37 | |
| 38 | ### Placement rules |
| 39 | |
| 40 | Set `x:DataType` **only where `BindingContext` is set**: |
| 41 | |
| 42 | 1. **Page / View root** — where you assign `BindingContext`. |
| 43 | 2. **DataTemplate** — which creates a new binding scope. |
| 44 | |
| 45 | Do **not** scatter `x:DataType` on arbitrary child elements. Adding |
| 46 | `x:DataType="x:Object"` on children to escape compiled bindings is an |
| 47 | anti-pattern — it disables compile-time checking and reintroduces reflection. |
| 48 | |
| 49 | ```xml |
| 50 | <!-- ✅ Correct: x:DataType at the page root --> |
| 51 | <ContentPage xmlns:vm="clr-namespace:MyApp.ViewModels" |
| 52 | x:DataType="vm:MainViewModel"> |
| 53 | <StackLayout> |
| 54 | <Label Text="{Binding Title}" /> |
| 55 | <Slider Value="{Binding Progress}" /> |
| 56 | </StackLayout> |
| 57 | </ContentPage> |
| 58 | |
| 59 | <!-- ❌ Wrong: x:DataType scattered on children --> |
| 60 | <ContentPage x:DataType="vm:MainViewModel"> |
| 61 | <StackLayout> |
| 62 | <Label Text="{Binding Title}" /> |
| 63 | <Slider x:DataType="x:Object" Value="{Binding Progress}" /> |
| 64 | </StackLayout> |
| 65 | </ContentPage> |
| 66 | ``` |
| 67 | |
| 68 | ### DataTemplate always needs its own x:DataType |
| 69 | |
| 70 | ```xml |
| 71 | <CollectionView ItemsSource="{Binding People}"> |
| 72 | <CollectionView.ItemTemplate> |
| 73 | <DataTemplate x:DataType="model:Person"> |
| 74 | <Label Text="{Binding FullName}" /> |
| 75 | </DataTemplate> |
| 76 | </CollectionView.ItemTemplate> |
| 77 | </CollectionView> |
| 78 | ``` |
| 79 | |
| 80 | ### Enforce binding warnings as errors |
| 81 | |
| 82 | | Warning | Meaning | |
| 83 | |---------|---------| |
| 84 | | **XC0022** | Binding path not found on the declared `x:DataType` | |
| 85 | | **XC0023** | Property is not bindable | |
| 86 | | **XC0024** | `x:DataType` type not found | |
| 87 | | **XC0025** | Binding used without `x:DataType` (non-compiled fallback) | |
| 88 | |
| 89 | Add to the `.csproj`: |
| 90 | |
| 91 | ```xml |
| 92 | <WarningsAsErrors>XC0022;XC0025</WarningsAsErrors> |
| 93 | ``` |
| 94 | |
| 95 | --- |
| 96 | |
| 97 | ## Binding Modes |
| 98 | |
| 99 | Set `Mode` explicitly **only** when overriding the default. Most properties |
| 100 | already have the correct default: |
| 101 | |
| 102 | | Mode | Direction | Use case | |
| 103 | |------|-----------|----------| |
| 104 | | `OneWay` | Source → Target | Display-only (default for most properties) | |
| 105 | | `TwoWay` | Source ↔ Target | Editable controls (`Entry.Text`, `Switch.IsToggled`) | |
| 106 | | `OneWayToSource` | Target → Source | Read user input without pushing back to UI | |
| 107 | | `OneTime` | Source → Target (once) | Static values; no change-tracking overhead | |
| 108 | |
| 109 | ```xml |
| 110 | <!-- ✅ Defaults — omit Mode --> |
| 111 | <Label Text="{Binding Score}" /> |
| 112 | <Entry Text="{Binding UserName}" /> |
| 113 | <Switch IsToggled="{Binding DarkMode}" /> |
| 114 | |
| 115 | <!-- ✅ Override only when needed --> |
| 116 | <Label Text="{Binding Title, Mode=OneTime}" /> |
| 117 | <Entry Text="{Binding SearchQuery, Mode=OneWayToSource}" /> |
| 118 | |
| 119 | <!-- ❌ Redundan |