goGud
goGud

Reputation: 4333

How to add items in listview in wpf application?

I am trying to add my items in listview. Here is my classes;

   FindFace.Show.Response response = await _api.Show_Face(lb_GalleryList.SelectedItem.ToString());        
   if (response.results.Count != 0)
   {
      List<FaceImages> faceImages = new List<FaceImages>();
      for (int i = 0; i < response.results.Count; i++)
      {
         faceImages.Add(new FaceImages() { Face_id = response.results[i].person_id.ToString(), Face_thumbnail = LoadImage(response.results[i].thumbnail) });
      }
      lv_Photos.ItemsSource = faceImages;                    
   }

In faceImages, here is what it looks like; enter image description here

And also here is my xaml file looks like;

<ListView x:Name="lv_Photos" HorizontalAlignment="Stretch" VerticalAlignment="Top">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="5" HorizontalAlignment="Stretch" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                        <Image Source="{Binding Face_thumbnail}" HorizontalAlignment="Stretch" VerticalAlignment="Top" Stretch="UniformToFill" />
                        <TextBlock Text="{Binding Face_id}" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" />
                    </StackPanel>
                </DataTemplate>
            </ListView>

However when I tried to put faceImages to ItemsSource in here;

lv_Photos.ItemsSource = faceImages;

Application gives

An exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll but was not handled in user code

Additional information: Items collection must be empty before using ItemsSource.

I didnt understand how I can pass faceImages class to my listview element.

Upvotes: 1

Views: 692

Answers (1)

Evk
Evk

Reputation: 101483

You have accidentally added your DataTemplate into ListView itself, as a child item. That's why Items collection is not empty and ItemsSource cannot be used, since they are mutually exclusive. Instead, use ListView.ItemTemplate:

<ListView x:Name="lv_Photos"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Top">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="5"
                         HorizontalAlignment="Stretch" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical"
                        VerticalAlignment="Stretch"
                        HorizontalAlignment="Stretch">
                <Image Source="{Binding Face_thumbnail}"
                       HorizontalAlignment="Stretch"
                       VerticalAlignment="Top"
                       Stretch="UniformToFill" />
                <TextBlock Text="{Binding Face_id}"
                           HorizontalAlignment="Stretch"
                           VerticalAlignment="Bottom" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Upvotes: 2

Related Questions