locked
How to set BindingContext to a page itself? RRS feed

  • Question

  • User210773 posted

    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?

    Sunday, June 19, 2016 2:32 PM

Answers

  • User129559 posted

    <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" x:Name="This"> <ContentPage.BindingContext> <x:Reference Name="This" /> </ContentPage.BindingContext>

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 11, 2017 9:46 PM

All replies

  • User37827 posted

    Are you looking for this?

    this.BindingContext = this;

    Sunday, June 19, 2016 4:50 PM
  • User210773 posted

    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.

    Sunday, June 19, 2016 5:58 PM
  • User66163 posted

    @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

    Sunday, June 19, 2016 6:53 PM
  • User210773 posted

    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.

    Sunday, June 19, 2016 7:28 PM
  • User253915 posted

    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();

    Tuesday, April 11, 2017 9:16 PM
  • User76049 posted

    @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.

    Tuesday, April 11, 2017 9:24 PM
  • User253915 posted

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

        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                BindingContext = new MainVM();
    
                InitializeComponent();
            }
        }
    
    Tuesday, April 11, 2017 9:26 PM
  • User76049 posted

    Try setting the binding context after InitializeComponent.

    Tuesday, April 11, 2017 9:27 PM
  • User253915 posted

    @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).

    Tuesday, April 11, 2017 9:34 PM
  • User76049 posted

    I do understand.

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

    Tuesday, April 11, 2017 9:43 PM
  • User129559 posted

    <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" x:Name="This"> <ContentPage.BindingContext> <x:Reference Name="This" /> </ContentPage.BindingContext>

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 11, 2017 9:46 PM
  • User253915 posted

    @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.

    Tuesday, April 11, 2017 9:57 PM
  • User76049 posted

    @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.

    Tuesday, April 11, 2017 10:01 PM
  • User253915 posted

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

    Tuesday, April 11, 2017 10:06 PM
  • User221952 posted

    " 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?

    Thursday, June 8, 2017 12:26 PM
  • User332065 posted

    BindingContext="{x:Reference MyContentPage}"

    where MyContentPage is the actual name of your content page.

    Tuesday, July 11, 2017 10:13 PM
  • User256826 posted

    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}}"/>
    ....
    
            
    
    ...
    
        
           
        
    
    
    
    Friday, November 24, 2017 6:48 PM
  • User382577 posted

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

    Friday, February 15, 2019 5:12 AM