If I use ContentPage.BindingContext in XAML to define my binding context then how do I access that in the C# back end?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



If I use ContentPage.BindingContext in XAML to define my binding context then how do I access that in the C# back end?



In my XAML I define the binding context like this:


<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Japanese;assembly=Japanese"
             xmlns:template="clr-namespace:Japanese.Templates"
             xmlns:viewModels="clr-namespace:Japanese.ViewModels; assembly=Japanese"
             x:Class="Japanese.Cards" Title="Binding Title">
<ContentPage.BindingContext>
     <viewModels:CardsViewModel />
    </ContentPage.BindingContext>
</ContentPage>



What I would like to know is how can I access this context in my C# back end.


public partial class Cards : ContentPage
{

public Cards()
    
     InitializeComponent();
        NavigationPage.SetBackButtonTitle(this, "Back");
    

    protected override void OnAppearing()
    {
     // I want to set some properties of the view here





Why not set bindingcontext in.cs file?
– Luminous_Dev
Aug 13 at 9:17




1 Answer
1



It seems you cannot give it an x:Name attribute like other elements in your XAML. In that case, your options are limited to declaring the object for your binding context in the code-behind, or referencing it from the BindingContext property.


x:Name


BindingContext



For the latter approach, do it like this:


protected override void OnAppearing()

var cardsViewModel = BindingContext as CardsViewModel;

if (cardsViewModel == null)
return;

cardsViewModel.Property = Value;



Earlier answer for reference:



You should be able to give it a name like so:


<ContentPage.BindingContext>
<viewModels:CardsViewModel x:Name="cardsViewModel" />
</ContentPage.BindingContext>



This will effectively just create a declaration like this in generated code:


private CardsViewModel cardsViewModel;



You can now access it in your code-behind:


protected override void OnAppearing()

cardsViewModel.Property = Value;





I added your code but it gives me an error saying: Error: The given key 'Xamarin.Forms.Xaml.ElementNode' was not present in the dictionary.
– Samantha J T Star
Aug 13 at 8:52





it seems to only be a problem when I added x:Name="cardsViewModel" without this I don’t get the error although I get other errors on the C# back end page. Do I need to initialize cardsViewModel in the C# backend?
– Samantha J T Star
Aug 13 at 8:54





Maybe I was mistaken and you cannot give it an x:Name element. Let me update.
– Gerald Versluis
Aug 13 at 8:55



x:Name






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS