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?
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}
not
{Binding Source={x:Reference root, Path=BindingContext.LoaderColor}
Notice the missing bracket after root.
Thanks once again NMackay!
Answers
@GaganSingh
Can you not just bind directly to the property assuming it's a public property in your viewmodel?
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.
Its inside of a listview. It will say that property does not exist inside the listview item object.
@GaganSingh
Ahh, ok.
try this.
No luck.
It says "Xamarin.Forms.Xaml.XamlParseException: No Property of name Path found"
@GaganSingh
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?
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.
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}
not
{Binding Source={x:Reference root, Path=BindingContext.LoaderColor}
Notice the missing bracket after root.
Thanks once again NMackay!
@GaganSingh
I didn't spot that, wish the parser was a bit stricter sometimes. Let's blame the parser
Glad you got it working.
Thanks again for all your help!
I never knew you could do Path=BindingContext.[property]
@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 aListView
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 theBindingContext
in the code of the view-model means that the following simple binding works fine:Hope this helps someone.
@EasyGoingPat
for me it didnt work defining as root. i had name the page and use it as