Cardview: Binding issue

Created on 22 Jun 2018  路  25Comments  路  Source: AndreiMisiukevich/CardView

When I attempt to bind to a property on the main page's view model, it doesn't work. I have created an example here...

https://github.com/robertmiles3/CardsViewBindingIssue

I have your CardsView library as the top example on the MainPage, and I have a CarouselView example below it. I like how your CardsView properly lays out in design mode while CarouselView doesn't layout properly. However, CarouselView properly binds to Stats.Count1 (etc) while I can't get your CardsView to bind like that.

Am I missing something in getting it to bind properly to the Stats property?

bug resolved

All 25 comments

I this case I suggest you to use ItemTemplate property for creating card template. All your cards are the same, but have got different bindingContext

Set ItemTemplate - your view, and Items - Stats collection.
And bind what you want))

Stats isn't a collection. It's a single object with properties on it. How would that work?

Redesign it to collection of int
new List()

I'm pulling a few counts as stats from a DB, so it doesn't make sense for it to be a list of ints.

By the way, make this class as IPropertyChanged

public class DashboardStats
{
    public int Count1 { get; set; }
    public int Count2 { get; set; }
    public int Count3 { get; set; }
    public int Count4 { get; set; }

    public int Count5 { get; set; }
    public int Count6 { get; set; }
    public int Count7 { get; set; }
    public int Count8 { get; set; }
}

I had that first at the beginning of my testing. Didn't work, so I moved it around testing different things.

So, your issue is clear for me, I'll try to help you =) I will look through your code/xaml this weekend

Thank you for feedback

I think, I know the reason...
Look here https://github.com/AndreiMisiukevich/CardView/blob/master/PanCardView/CardsView.cs#L1068-L1103

if you doesn't use itemTemplate, your view has itself as bindingContext..
It seems like a bug, what do you think, colleague?

https://github.com/AndreiMisiukevich/CardView/blob/master/PanCardView/CardsView.cs#L1031

if view == context, we needn't set it as context.. maybe it's the reason of our headache)

Probably, you want to test this case and contribute to this project? Or you can wait until I fix it myself

Hmm, yes possibly so. So, what would be the fix? Can it somehow just use the parent/page binding context?

I believe if we leave BindingContext as null the view will get parent's context

Gotcha. I'll try that in a bit.

And it must be here https://github.com/AndreiMisiukevich/CardView/blob/master/PanCardView/CardsView.cs#L526
We should to extend this check

        if (CurrentView.BindingContext == context || CurrentView == context)
        {
            return false;
        }

So, only two check (the first is above) and the second is below

https://github.com/AndreiMisiukevich/CardView/blob/master/PanCardView/CardsView.cs#L1029

        if (view != null && view != context)
        {
            view.BindingContext = context;
        }

Those 2 changes (L526 and L1029) worked. You want to put those in or want me to submit a PR?

Create PR please

I will create new pack on weekend

I just ran into this issue where I set the BindingContext to my main view. When I load the CarouselView, it sets it to the main view, then overrides it to the view set.

I was able to temporarily fix this by
protected override void OnParentSet()
{
base.OnParentSet();
Debug.WriteLine("Done");
BindingContext = Navigation.NavigationStack[2];
}

but when you change views, it will reset again.

Sorry, I didn't understand your matter.

But it doesn't seem to be the same issue

@ryanherman I validated all my code and didn't find any line, where I set BindingContext for Carousel/CardsView. I set it only for card elements.

Can you reproduce this issue with latest code?

Awesome, thanks. Just updated in my main app and it resolved my issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alicint picture alicint  路  3Comments

troyvnit picture troyvnit  路  8Comments

LeoJHarris picture LeoJHarris  路  3Comments

ssotir picture ssotir  路  7Comments

morhung picture morhung  路  7Comments