XAML Binding to rootObject

NoelHeesenNoelHeesen NLMember
edited November 2016 in Cross Platform with Xamarin

Hello,

I'm working on an App and need to get the BindingContext of the rootObject in my XAML from within a DataTemplate.
I have found that when directly assigning the Page to the App.MainPage property everything goes well and the picker finds the bindingcontext of the rootOBject by using x:Reference.

<ContentPage x:Class="MyNamespace.MyPage" x:Name="MyPageName">

    <ListView ItemsSource="{Binding MyListViewItems}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <controls:BindablePicker ItemsSource={Binding Source={x:Reference MyPageName}, Path=BindingContext.MyProp} />
                </ViewCell>
            </DataTemplate>
        <ListView.ItemTemplate>
    </ListView>

</ContentPage>

Whenever I put this page inside a tabbed page like this and assign the MasterDetail to App.MainPage the x:Reference can no longer find the element name 'MyPageName'.

<MasterDetailPage>
    <TabbedPage>
        <views:MyPage Title="MyPageTitle" />
    </TabbedPage>
</MasterDetailPage>

The weird thing is that it works when I use MainPage = new MyPage(); but not when it's declared in XAML within another page. I'm so confused and no where near a solution :(

Does anyone have an idea what's going on here?

Kind Regards,

Answers

  • AlexDunnAlexDunn USMember ✭✭✭

    It's based on what is instantiating the ContentPage. Because your TabbedPage is instantiating it as a child rather than as a root page, it's ignoring your local attributes on your ContentPage node.

    Have you tried adding x:Name="MyPageName" to your TabbedPage child version?
    Like:

    <MasterDetailPage>
        <TabbedPage>
            <views:MyPage x:Name="MyPageName" Title="MyPageTitle" />
        </TabbedPage>
    </MasterDetailPage>
    
  • NoelHeesenNoelHeesen NLMember

    @AlexDunn I will try this out as soon as I can and let you know. Thanks for helping out!

Sign In or Register to comment.