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?
<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>
Answers
Are you looking for this?
this.BindingContext = this;
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.
@hz10
ContentPage has a BindingContext attribute that may be relevant. Not sure, but try something like this:
Hope this helps,
Tim
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.
FWIW: I've tried the same thing with no luck. I'm still stuck assigning the BindingContext in the code-behind:
This fails:
As does this:
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();
@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.
@NMackay If you mean the page constructor, yes, it is public:
Try setting the binding context after InitializeComponent.
@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).I do understand.
Your page needs to know what it's binding to as it's a view 1st approach, ViewModelLocator pattern for example.
@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.
@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.
@AdamMeaney
Tried your suggestion but XS won't accept it. It highlights the "x:Reference" and says "Could not find namespace for x:Reference"
" 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?
BindingContext="{x:Reference MyContentPage}"
where MyContentPage is the actual name of your content page.
BindingContext can be defined as an ContentPage Attribute, as a property-element, or as a resource in a ResourceDictionary:
JafetGranados.3096, thank You!
Your answer is the most complete and correct.