Back to results for “Advanced Data Binding & Collections”
.NET MAUI › Advanced Data Binding & Collections

What is RelativeSource binding and when do you need it?

Intermediate

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.

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