How to set BindingContext to a page itself?

hz10hz10 USMember ✭✭
edited June 2016 in Xamarin.Forms

In regular XAML, it is very straightforward:
DataContext="{Binding RelativeSource={RelativeSource Mode=Self} }

Could anyone offer a tip on how to do it for a Xamarin Forms page?

Tagged:

Best Answer

Answers

  • MichaelOConnorMichaelOConnor USUniversity ✭✭

    Are you looking for this?

    this.BindingContext = this;

  • hz10hz10 USMember ✭✭

    Sorry, Michael, I should have stated specifically that I was looking for a way to set BindingContext in XAML. I am currently using:
    BindingContext = this;

    in the constructor. I prefer setting all contexts in XAML. That is how I have been doing with regular XAML pages.

  • HunumanHunuman GBMember ✭✭✭✭
    edited January 2018

    @hz10

    ContentPage has a BindingContext attribute that may be relevant. Not sure, but try something like this:

    <ContentPage x:Class="TestPage"
                 xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 Title="Test Page" BindingContext="Whatever_you_need">
    

    Hope this helps,

    Tim

  • hz10hz10 USMember ✭✭
    edited June 2016

    Sorry, Tim, it does not work.

    The following does not work either:
    <ContentPage.BindingContext> <local:Mypage /> </ContentPage.BindingContext>
    I got that from an example in a Xamarin official document.

  • RodBarnesRodBarnes USMember ✭✭
    edited January 2018

    FWIW: I've tried the same thing with no luck. I'm still stuck assigning the BindingContext in the code-behind:

    This fails:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            xmlns:local="clr-namespace:REST_demo" 
            x:Class="REST_demo.MainPage">
        <ContentPage.BindingContext>
            <local:ViewModel.MainVM />
        </ContentPage.BindingContext>
    

    As does this:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            xmlns:local="clr-namespace:REST_demo" 
            xmlns:VM="clr-namespace:REST_demo.ViewModels; assembly=REST_demo.ViewModels"
            x:Class="REST_demo.MainPage">
        <ContentPage.BindingContext>
            <VM:MainVM />
        </ContentPage.BindingContext>
    

    An attempt to run the solution presents the message:

    Type local:ViewModel.MainVM not found in xmlns clr-namespace:REST_demo
    Or
    Type VM:MainVM not found in xmlns clr-namespace:REST_demo.ViewModels; assembly=REST_demo.ViewModels

    But the following still works from within the code-behind. I was just hoping to eliminate any need for any extra code in the code-behind.

    BindingContext = new MainVM();

  • NMackayNMackay GBInsider, University mod

    @RodBarnes

    Is the constructor public/internal?

    I've noticed something with XamlC you have to make stuff public and after successful compilation you can make it private if appropriate.

  • RodBarnesRodBarnes USMember ✭✭

    @NMackay If you mean the page constructor, yes, it is public:

        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                BindingContext = new MainVM();
    
                InitializeComponent();
            }
        }
    
  • NMackayNMackay GBInsider, University mod

    Try setting the binding context after InitializeComponent.

  • RodBarnesRodBarnes USMember ✭✭
    edited April 2017

    @NMackay I think you misunderstand. Setting the BindingContext inside of the code-behind works. I'm trying to set it inside the XAML as shown in the page referenced by hz10 (above).

  • NMackayNMackay GBInsider, University mod

    I do understand.

    Your page needs to know what it's binding to as it's a view 1st approach, ViewModelLocator pattern for example.

  • RodBarnesRodBarnes USMember ✭✭
    edited April 2017

    @NMackay I do not understand your point. If you look at the page provided by Xamarin, I am following what it demonstrates. If BindingContext is set it in the XAML, how would one cause this to be acquired after InitializeComponent?

    In the code-behind, setting BindingContext before InitializeComponent works fine.

  • NMackayNMackay GBInsider, University mod

    @RodBarnes

    I don't know what your page is doing, I just know from experience that setting the binding context before initializing the page can cause issues.

    Anyway, as a fellow biker (well for 15 years anyway)...peace.

  • RodBarnesRodBarnes USMember ✭✭

    @AdamMeaney
    Tried your suggestion but XS won't accept it. It highlights the "x:Reference" and says "Could not find namespace for x:Reference"

  • stanleybroostanleybroo USMember ✭✭

    " I just know from experience that setting the binding context before initializing the page can cause issues."

    Saved my day ;-) ..dont know why tutorials out there is showing it the other way ..doin it before initializing?

  • JafetGranados.3096JafetGranados.3096 USMember ✭✭
    edited November 2017

    BindingContext can be defined as an ContentPage Attribute, as a property-element, or as a resource in a ResourceDictionary:

    
    ...
     xmlns:ViewModels="clr-namespace:MyNameSpace.ViewModels"
     BindingContext="{Binding Source={ViewModels:MyViewModel}}"/>
    ....
    <ContentPage.BindingContext>
            <ViewModels:MyViewModel />
    </ContentPage.BindingContext>
    ...
    <ContentPage.Resources>
        <ResourceDictionary>
           <ViewModels:MyViewModel x:Key="MyViewModelKey"/>
        </ResourceDictionary>
    </ContentPage.Resources>
    
    
  • SvarogichSvarogich Member

    JafetGranados.3096, thank You!
    Your answer is the most complete and correct.

Sign In or Register to comment.