Ibrahim
Ibrahim

Reputation: 969

XAML Binding to parent of data object Xamarin Forms

Xamarin Forms (XAML): How do I access the “parent” data context?

Upvotes: 1

Views: 8430

Answers (2)

LozCool
LozCool

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

Vincent Poirier
Vincent Poirier

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}" />

Reference: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/data_binding_basics/

Upvotes: 0

Related Questions