Forum General

Announcement:

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.

Observable Collection not displaying images or labels in Collection View

GibGib Member ✭✭

I have an observable collection that is full of images but when I bind it to a collection view nothing is showing up.

Here is my xamal:

            <CollectionView
                    ItemsSource="{Binding AttachmentList}"
                    SelectedItem="{Binding SelectedItem}"
                    SelectionMode="Single"
                    SelectionChanged="OnSelectionChanged"
                    Margin="10"
                    EmptyView="this is empty for some reason"
                    >

                <CollectionView.ItemTemplate>
                    <DataTemplate>

                        <Grid Margin="0,0,0,10">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="0.5*"/>
                                <ColumnDefinition Width="0.5*"/>
                            </Grid.ColumnDefinitions>

                            <Label
                                Grid.Column="1"
                                Text="{Binding FileName}"
                                FontSize="Small"
                                HorizontalOptions="Start"
                                VerticalOptions="Start"
                                />
                            <Image
                                Grid.Column="2"
                                Source="{Binding PhotoSource}"
                                HorizontalOptions="Start"
                                VerticalOptions="Start"
                                />

                        </Grid>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

Here is the load function that puts all the images from the database into an observable collection:

private async Task LoadPictures(ChattelMobileContext context)
        {
            // We could have deleted the local database, avoid the "No such table: SubjectAttachment" error.
            if (string.IsNullOrEmpty(context.DBPath))
            {                
                await DisplayAlert("No Subject", "Please select a subject before trying to take a picture.", "Ok");
            }
            else
            {
                // Create an anonymous object to be displayed in the collectionview.
                var attachments = await context.SubjectAttachment
                    .Where(sa => sa.SubjectId == _subjectId)
                    .OrderBy(sa => sa.FileName)
                    .Select(se =>
                        new
                        {
                            se.FileName,
                            PhotoSource = ImageUtils.ByteArrayToImageSource(se.FileContents)
                        }
                    )
                    .ToListAsync();

                AttachmentList = new ObservableCollection<object>(attachments);
            }
        }

The AttachmentList is filling up with values that match what I am binding to in the xamal so I think that pictures are saving correctly after they are taken and then correctly being put into the observable collection but nothing shows up when I bind it. The empty listview message shows up, does this mean that it thinks I am binding and empty list? Still relatively new to xamarin but I have been stuck on this for a few days so I thought I would ask on here.

Best Answer

  • GibGib Member ✭✭
    Accepted Answer

    I ended up fixing this by adding an on property changed to the AttachmentList and making it a full property.

Answers

  • GibGib Member ✭✭
    Accepted Answer

    I ended up fixing this by adding an on property changed to the AttachmentList and making it a full property.

Sign In or Register to comment.