Xamarin Forms ViewCell not displaying binded data with list but works with tuple

469 Views Asked by At

Strange behaviour when I try to wire up a viewcell in Xamarin Forms to show data from the BindingContext.

Basically I populate an object to hold 3 images (besides other things) - I'm new to Xamarin and thought I'd try to figure out how to build an instagram-esque photo gallery where I have 3 clickable images per row and make the UI snappy and responsive without lag and memory issues. Exploring doing this with listview perhaps so I can get the viewcell reuse feature??...

enter image description here

Anyway, I pass this object via BindingContext to the custom view cell. The images are showing something when using tuples. But if I use a list item it doesn't.

I don't understand why it would show with one and not the other - both tuples and the list are populated at the same time and hold the info?

public class ExploreVCell : ViewCell
{
    public ExploreVCell()
    {
        Image image1 = new Image();
        Image image2 = new Image();
        Image image3 = new Image();

       // List<StylePost> list;

        Label lbl = new Label ();

        Grid grid = new Grid
        {
            Padding = new Thickness(10),
            RowDefinitions = { new RowDefinition { Height = GridLength.Star } },
            ColumnDefinitions = {
                new ColumnDefinition { Width = GridLength.Star },
                new ColumnDefinition { Width = GridLength.Star },
                new ColumnDefinition { Width = GridLength.Star }
            },
            BackgroundColor=Color.Blue
        };

        grid.Children.Add(image1, 0, 0);
        grid.Children.Add(image2, 1, 0);
        grid.Children.Add(image3, 2, 0);

        View = grid;

        image1.SetBinding<ListViewRow>(Image.SourceProperty, i => i.MyTuple.Item1.ImageUrl);
        image2.SetBinding<ListViewRow>(Image.SourceProperty, i => i.MyTuple.Item2.ImageUrl);
        image3.SetBinding<ListViewRow>(Image.SourceProperty, i => i.MyTuple.Item3.ImageUrl);

The above works but not when I swap out the images bindings to being set via the below:

        // doesnt work with these lines - weird
        image1.SetBinding<ListViewRow>(Image.SourceProperty, i => i.RowData[0].ImageUrl);
        image2.SetBinding<ListViewRow>(Image.SourceProperty, i => i.RowData[1].ImageUrl);
        image3.SetBinding<ListViewRow>(Image.SourceProperty, i => i.RowData[2].ImageUrl);

This is the class that is bound to the viewcell after I have stuffed data into it. As you can see I'm using an ObservableCollection but it's the same behaviour with a List...only a tuple seems to work. It's fine for now but what if I need to use enumerated methods :(

public class ListViewRow
{
    public ListViewRow(StylePost p, StylePost q, StylePost r)
    {
        MyTuple = new Tuple<StylePost, StylePost, StylePost>(p, q, r);
        RowData = new ObservableCollection<StylePost>();
        RowData.Add(p);
        RowData.Add(q);
        RowData.Add(r);

    }
    public Tuple<StylePost, StylePost, StylePost> MyTuple { get; set; }
    public ObservableCollection<StylePost> RowData { get; set; }

}
0

There are 0 best solutions below