Forum Xamarin.Forms


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Binding to viewmodel property instead of listview's bindingcontext

GaganSinghGaganSingh USUniversity ✭✭✭

I have a listview with a button that has a backgroundcolor property that needs to be bound to the viewmodel property.
My Viewmodel is set in code

public AccountsPage() { InitializeComponent(); BindingContext = App.Locator.Accounts; }

The following code gives an error that says the LoaderColor doesnt exist in AccountsPage.cs.

<Button Text="Test" 
BackgroundColor="{Binding Source={x:Reference root, Path=LoaderColor}" />

(root is the name of the ContentPage)

I was only able to get it working by doing the following, but it looks dirty. :(

<Button Text="Test" BackgroundColor="{Binding Source={x:Static viewmodel:AccountsViewModel.LoaderColor}" />

How do i specify the BindingContext the backgroundcolor property should reference?

Best Answer


  • NMackayNMackay GBInsider, University admin
    edited May 2016


    Can you not just bind directly to the property assuming it's a public property in your viewmodel?

    <Button Text="Test" 
    BackgroundColor="{Binding LoaderColor}" />

    As your binding context is the viewmodel the binding will just look there.

    I have a Busy property in my VM to control the ActivityIndicator. I can just bind like this.

    <ActivityIndicator IsRunning="{Binding Busy}"
                                 IsVisible="{Binding Busy}"
                                 AbsoluteLayout.LayoutFlags="PositionProportional" AbsoluteLayout.LayoutBounds="0.5,0.5,-1,-1"
  • GaganSinghGaganSingh USUniversity ✭✭✭

    Its inside of a listview. It will say that property does not exist inside the listview item object.

  • NMackayNMackay GBInsider, University admin


    Ahh, ok.

    try this.

    <Button Text="Test" 
    BackgroundColor="{Binding Source={x:Reference root, Path=BindingContext.LoaderColor}" />
  • GaganSinghGaganSingh USUniversity ✭✭✭

    No luck.
    It says "Xamarin.Forms.Xaml.XamlParseException: No Property of name Path found"

  • NMackayNMackay GBInsider, University admin


    Odd, that syntax is correct as I checked and tested against my app. The only difference in my app is that I'm instantiating my BindingContext in XAML. What if you set the binding context before you InitializeComponent in your page constructor?

  • GaganSinghGaganSingh USUniversity ✭✭✭

    Still no luck.. I get the same error. maybe Path=BindingContext.LoaderColor is only recognized if i set it in xaml. Ill give it a shot.

  • NMackayNMackay GBInsider, University admin


    I didn't spot that, wish the parser was a bit stricter sometimes. Let's blame the parser :smile:

    Glad you got it working.

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭✭

    @GaganSingh @NMackay Sorry for crashing the discussion, but I just wanted to add something that is probably obvious but worth pointing out for anyone reading this thread.

    I followed the discussion above and it helped a lot, but I still could not get a binding to work when I set the BindingContext to the view-model in code. I was trying to bind a ListView to a read-only array of strings created in the view-model constructor. The problem turned out to be that I had used a public field instead of a public property with a backing field. I am not entirely sure why this makes a difference, but using a public property with backing field and setting the BindingContext in the code of the view-model means that the following simple binding works fine:

    <ListView x:Name="listDeviceHistory" ItemsSource="{Binding RecentDevices}">

    Hope this helps someone.


  • batmacibatmaci DEMember ✭✭✭✭✭

    @GaganSingh said:
    im an idiot... i forgot to remove the static from the LoaderColor property
    and it should be
    {Binding Source={x:Reference root}, Path=BindingContext.LoaderColor}


    {Binding Source={x:Reference root, Path=BindingContext.LoaderColor}

    Notice the missing bracket after root.

    Thanks once again NMackay!

    for me it didnt work defining as root. i had name the page and use it as

    {Binding Source={x:Reference Name=myPage}, Path=BindingContext.LoaderColor}
Sign In or Register to comment.