I'm trying to show a set of pictures from a directory in a ListBox. This ListBox should be updated when I select another directory. The following code does not work correcty and I don't know why. LoadImages() works correctly because _selectedImageList has the elements contained in the selected directory but the ListBox does not show anything.
XAML:
<ListBox x:Name="ListBoxSnapshots" ItemsSource="{Binding SelectedImageList, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Focusable="False" BorderThickness="0" SelectionMode="Single" HorizontalContentAlignment="Left" VerticalContentAlignment="Stretch" SelectionChanged="ListBoxSnapshots_SelectionChanged" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Disabled" SelectedIndex="0" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding}" Stretch="Fill" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
My ViewModel:
private ObservableCollection<ImageSource> _selectedImageList = new ObservableCollection<ImageSource>();
public ObservableCollection<ImageSource> SelectedImageList
{
get { return _selectedImageList; }
set { _selectedImageList = value; }
}
private void LoadImages()
{
_selectedImageList = new ObservableCollection<ImageSource>();
DirectoryInfo aSnapshotTempDir = new DirectoryInfo(@"..\..\Images");
foreach (FileInfo aFile in aSnapshotTempDir.GetFiles("*.jpg"))
{
Uri uri = new Uri(aFile.FullName);
_selectedImageList.Add( new BitmapImage( uri ) );
}
}
I'm trying to do something like that ( Displaying Images in ListView (or something better!) in WPF MVVM using databinding ) but something is wrong in my code.
Thanks.