It should be very simple, but I can't get it to work ...
CustomItem is a class with the ThumbnailImage property. I'm trying to bind an ObservableCollection to a ListBox to display images. This is my code:
public ObservableCollection<CustomItem> AvailableItems { get; set; }
<ListBox Width="103" Height="480" ItemsSource="{Binding AvailableItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1">
<ContentControl Content="{Binding Path=ThumbnailImage}"
Width="100" Height="100" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
CustomItem is as follows
public class CustomItem
public Image ThumbnailImage { get; set; }
}
Nothing appears in the ListBox at startup. Any idea what goes wrong? Thank!
- Change 1 - I assume that I can say that debugging is that closet.Items.Count == 0 when AvailableItems.Count == 5. I tried adding ItemsSource = "{Binding AvailableItems, UpdateSourceTrigger = PropertyChanged} ", but it did not help: (
- Change 2 -
I did the following in my XAML
DataContext="{Binding RelativeSource={RelativeSource Self}}"
instead, when I did the following in code, everything worked:
DataContext = this;