Advertisement
Intermediate .NET MAUI, Advanced Data Binding & Collections

What is RelativeSource binding and when do you need it?

Short answer

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.

Save .NET MAUI mock interview
Advertisement
AModel answer
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.
Likely follow-ups
  • Why is this needed in templates? | What are the RelativeSource modes?
Related topics