0

I'm trying to create a ListView that displays an ObservableCollection and regardless of what I do with the ItemTemplate I can't get the image to actually display. Most of what I've been able to find deals with displaying an image as a property of the collection, but not when it is the collection. I have a feeling I'm just doing something stupid but I'm not able to figure it out on my own.

<ListView Visibility="{Binding HasImages, Converter={StaticResource BooleanToVisibleConverter}}" Height="Auto" ItemsSource="{Binding Images}">
 <ListView.ItemTemplate>
     <DataTemplate>
         <Image></Image>
     </DataTemplate>
 </ListView.ItemTemplate>
 </ListView>

private ObservableCollection<BitmapImage> _Images = new ObservableCollection<BitmapImage>();
public ObservableCollection<BitmapImage> Images
{
    get { return _Images; }
    set { _Images = value; RaisePropertyChanged(); }
}
Isma
  • 14,604
  • 5
  • 37
  • 51
Chris
  • 733
  • 1
  • 5
  • 19
  • Can you also post your "Images" property of your ViewModel? – Isma May 03 '18 at 19:43
  • Shouldn't your have a source="{binding}"? – Kevin Cook May 03 '18 at 19:55
  • @KevinCook: I tried that and the application enters a break state. – Chris May 03 '18 at 19:58
  • I tried your code and it's working, I just had to add source="{binding}" as mentioned by @KevinCook. Maybe the problem is in your converter or the way you load the images... – Isma May 03 '18 at 19:59
  • It's not the converter because if I replace the `` with a `` I get the appropriate number of TextBlocks. I will look into how I load the images, however. – Chris May 03 '18 at 20:02
  • You might need to convert the bitmapimage into an imagesource with a valueconverter, – Kevin Cook May 03 '18 at 20:02
  • @KevinCook BitmapImage is derived from ImageSource. No conversion necessary. – Clemens May 03 '18 at 20:26
  • All you need is `` in the DataTemplate. If that doesn't work, there is a problem that is out of the scope of this question. – Clemens May 03 '18 at 20:28
  • I figured it out. I was loading the BitmapImages on a different thread than the UI thread and when the UI thread was trying to gain access it was unable to. – Chris May 03 '18 at 20:46
  • Then just call Freeze on the BitmapImages to make them cross-thread accessible. – Clemens May 03 '18 at 21:12

0 Answers0