Reputation: 969
Xamarin Forms (XAML): How do I access the “parent” data context?
Upvotes: 1
Views: 8430
Reputation: 89
I found the 'nice' way to do this in Xamarin is to name the 'root' BindingContext on each page:
<ContentPage.BindingContext>
<AViewModel x:Name="RootContext" />
</ContentPage.BindingContext>
And then reference it in the element that is needed, without having to set an individual BindingContext. For example:
<Button Command="{Binding Source={x:Reference RootContext}, Path=StopCommand}"/>
A binding context is OTT sometimes, because everything below inherits it (sometimes without you knowing).
Upvotes: 3
Reputation: 4316
Have you tried the {x:Reference parent}
markup?
I'm new at this myself, but if your element requires element to only one element (the parent in your case), you should be able to specify it.
In this example, you'd have to replace label
by the parent element and from there the Binding
would reference the right object.
<Slider x:Name="rotationXSlider"
BindingContext="{x:Reference label}"
Grid.Row="3" Grid.Column="1"
Maximum="360"
Value="{Binding RotationX, Mode=OneWayToSource}" />
Upvotes: 0