.NET MAUI

Advanced Data Binding & Collections

6 question(s)

What is ObservableCollection and why use it for lists?

Beginner
ObservableCollection<T> implements INotifyCollectionChanged, so adding/removing items automatically updates a bound CollectionView without rebinding. Use it for dynamic lists. Note that replacing the whole collection still requires a property change notification on the property holding it.
public ObservableCollection<Item> Items { get; } = new();
Items.Add(newItem);   // UI updates automatically
Real-world example A chat list appends incoming messages to an ObservableCollection and they appear instantly without refreshing the whole view.

Common follow-ups: Does it notify on item property changes? | What if you replace the collection?

Collections Data Binding & MVVM Advanced Data Binding & Collections

How do you bind to nested properties and handle null paths safely?

Intermediate
Use dot paths like {Binding Order.Customer.Name}. If intermediate values can be null, bindings resolve to empty rather than throwing, but you can supply FallbackValue and TargetNullValue for clarity. Compiled bindings (x:DataType) validate the path at build time.
<Label Text="{Binding Order.Customer.Name,
    TargetNullValue='Unknown', FallbackValue='—'}" />
Real-world example A summary label shows 'Unknown' when a customer isn't loaded yet, avoiding a blank UI.

Common follow-ups: What is FallbackValue vs TargetNullValue? | Do null paths throw?

Data Binding & MVVM Advanced Data Binding & Collections XAML & UI Layouts

What is RelativeSource binding and when do you need it?

Intermediate
RelativeSource binds to something other than BindingContext—Self (the element), an ancestor of a given type, or the templated parent. It's essential inside DataTemplates to reach the parent page's view-model command rather than the item's context.
<Button Text="Delete"
    Command="{Binding Source={RelativeSource
        AncestorType={x:Type ContentPage}},
        Path=BindingContext.DeleteCommand}"
    CommandParameter="{Binding .}" />
Real-world example A delete button inside a list item invokes the page view model's command via AncestorType while passing the item as a parameter.

Common follow-ups: Why is this needed in templates? | What are the RelativeSource modes?

Data Binding & MVVM CollectionView Advanced Data Binding & Collections

How do you implement input validation with data binding?

Intermediate
Expose validation state on the view model (per-field error properties or INotifyDataErrorInfo), bind UI to show errors (DataTrigger or an error label), and disable submit via a command's CanExecute. The CommunityToolkit and behaviors package also offer validation behaviors for common rules.
public bool HasEmailError => string.IsNullOrEmpty(Email) ||
    !Email.Contains('@');
// Submit command CanExecute returns !HasEmailError && ...
Real-world example A signup form highlights invalid fields and keeps the Create button disabled until all validations pass.

Common follow-ups: What is INotifyDataErrorInfo? | How do you disable submit on errors?

Validation Data Binding & MVVM Advanced Data Binding & Collections

How do you efficiently update large observable collections?

Advanced
ObservableCollection raises a change event per item, which is costly for bulk updates. For large refreshes, replace the collection reference (and notify), use a batching/optimized ObservableRangeCollection (from the toolkit) with AddRange, or suspend notifications. Also prefer incremental loading for very large data.
// ObservableRangeCollection (MAUI Community Toolkit)
Items.AddRange(newBatch);   // one notification instead of N
Real-world example Loading 500 rows via AddRange updates the CollectionView once instead of triggering 500 layout passes.

Common follow-ups: Why is per-item notification slow? | What is incremental loading?

Collections Performance & Optimization Advanced Data Binding & Collections

How do you implement grouping in a CollectionView?

Advanced
Set IsGrouped="True" and bind ItemsSource to a collection of groups (each an IEnumerable, often a class deriving from List<T> with a key). Provide GroupHeaderTemplate for headers. The view model shapes flat data into grouped structures, typically with LINQ GroupBy.
<CollectionView ItemsSource="{Binding Groups}" IsGrouped="True">
    <CollectionView.GroupHeaderTemplate>
        <DataTemplate><Label Text="{Binding Key}" FontAttributes="Bold" /></DataTemplate>
    </CollectionView.GroupHeaderTemplate>
</CollectionView>
Real-world example A contacts list groups people under alphabetical headers built with LINQ GroupBy in the view model.

Common follow-ups: How is grouped data shaped? | What template renders headers?

CollectionView Collections Advanced Data Binding & Collections