1

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.

4
  • You have something in ListView.Items (so for example you manually added some items in ListView in xaml or somehow added something to Items collection in code before), so you cannot use ItemsSource, because Items and ItemsSource are mutually exclusive. Commented Mar 2, 2017 at 10:13
  • @Evk I am trying to find some solution for this case, I can rewrite everything if necessary. Commented Mar 2, 2017 at 10:15
  • also should be using ObservableCollection not List as itemSource or binding wont update Commented Mar 2, 2017 at 10:25
  • If you don't ever remove or add items to collection but only update it as a whole (like in your example) - you don't need ObservableCollection. Commented Mar 2, 2017 at 10:34

1 Answer 1

2

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> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.