.NET MAUI
Data Binding & MVVM
7 question(s)
What is data binding in MAUI?
Beginner
Data binding connects a target property on a UI element to a source property on another object (usually a view model), so changes propagate automatically. You set BindingContext and use {Binding Path}. Bindings can be OneWay, TwoWay, or OneTime, reducing manual UI-update code.
<Entry Text="{Binding Username, Mode=TwoWay}" />
<Label Text="{Binding Greeting}" />
Real-world example
Typing into an Entry updates the view model's Username, and the view model computes a Greeting that the Label shows—no event handlers needed.
Common follow-ups: What binding modes exist? | What is BindingContext?
MVVM
INotifyPropertyChanged
XAML & UI Layouts
What is the MVVM pattern and why use it in MAUI?
Beginner
MVVM (Model-View-ViewModel) separates the UI (View) from presentation logic (ViewModel) and data (Model). The View binds to the ViewModel, which exposes properties and commands. It improves testability (view models are plain classes), reusability, and lets designers and developers work in parallel. MAUI is built around MVVM.
Real-world example
A team unit-tests all screen logic by instantiating view models directly, with no UI or emulator required.
Common follow-ups: How does the View talk to the ViewModel? | What goes in the Model?
MVVM
Data Binding & MVVM
Testing & Debugging
What is INotifyPropertyChanged and why is it required?
Intermediate
INotifyPropertyChanged defines the PropertyChanged event that a binding source raises when a property value changes, telling the UI to refresh. Without it, one-way and two-way bindings to view-model properties won't update the UI after the initial value. Each settable bound property must raise it.
public class MainViewModel : INotifyPropertyChanged
{
string _name;
public string Name
{
get => _name;
set { _name = value;
PropertyChanged?.Invoke(this,
new PropertyChangedEventArgs(nameof(Name))); }
}
public event PropertyChangedEventHandler PropertyChanged;
}
Real-world example
A live-updating price label refreshes only because the view model raises PropertyChanged when the price field changes.
Common follow-ups: How does CommunityToolkit.Mvvm reduce this boilerplate? | Which binding modes need it?
MVVM
Data Binding & MVVM
MVVM Toolkit
How does the CommunityToolkit.Mvvm reduce MVVM boilerplate?
Intermediate
CommunityToolkit.Mvvm provides source generators. [ObservableProperty] on a partial field generates a full property with change notification; [RelayCommand] on a method generates an ICommand. ObservableObject supplies INotifyPropertyChanged. This removes most hand-written property and command code.
public partial class LoginViewModel : ObservableObject
{
[ObservableProperty] string username;
[RelayCommand]
async Task LoginAsync() => await Shell.Current.GoToAsync("//home");
}
Real-world example
A team cuts a 200-line view model to 60 lines by adopting the toolkit's attributes, with fewer bugs from forgotten notifications.
Common follow-ups: What does [RelayCommand] generate? | What is ObservableObject?
MVVM Toolkit
Commands
Data Binding & MVVM
What is ICommand and how do commands work in MVVM?
Intermediate
ICommand exposes Execute, CanExecute, and CanExecuteChanged. Controls like Button bind their Command property to a view-model command, keeping logic out of code-behind. CanExecute enables/disables the control automatically. Command and RelayCommand implementations wrap methods and optional predicates.
[RelayCommand(CanExecute = nameof(CanSave))]
async Task SaveAsync() => await _repo.SaveAsync(Item);
bool CanSave() => !string.IsNullOrEmpty(Item.Name);
Real-world example
A Save button auto-disables until the form is valid because its bound command's CanExecute returns false for empty input.
Common follow-ups: How do you pass a CommandParameter? | How do you re-evaluate CanExecute?
Commands
MVVM Toolkit
Controls & Views
How do you implement value conversion in bindings?
Advanced
Implement IValueConverter (Convert and ConvertBack) to transform a source value into a target-friendly value, e.g., bool to color or enum to text. Declare the converter as a resource and reference it via Converter= in the binding. Use ConvertBack for two-way scenarios; return BindableProperty.UnsetValue to skip.
public class BoolToColorConverter : IValueConverter
{
public object Convert(object v, Type t, object p, CultureInfo c)
=> (bool)v ? Colors.Green : Colors.Red;
public object ConvertBack(object v, Type t, object p, CultureInfo c)
=> throw new NotSupportedException();
}
Real-world example
A status dot turns green or red by binding IsOnline through a BoolToColorConverter, keeping the view model free of UI colors.
Common follow-ups: When do you need ConvertBack? | How do you pass a ConverterParameter?
Data Binding & MVVM
Converters
Styling
What are compiled bindings and why use them?
Advanced
Compiled bindings (x:DataType on a view) resolve binding paths at compile time instead of via reflection at runtime. They give compile-time error checking, better performance, and IntelliSense. Set x:DataType to the view-model type; MAUI generates efficient get/set code. They're recommended for all production bindings.
<ContentPage xmlns:vm="clr-namespace:MyApp.ViewModels"
x:DataType="vm:MainViewModel">
<Label Text="{Binding Title}" />
</ContentPage>
Real-world example
Enabling compiled bindings surfaces a typo in a binding path as a build error instead of a silent blank label at runtime.
Common follow-ups: How do compiled bindings improve performance? | What happens if the path is wrong?
Data Binding & MVVM
Performance & Optimization
XAML & UI Layouts