Click here to Skip to main content
13,095,658 members (55,524 online)
Rate this:
Please Sign up or sign in to vote.
Hi i'm trying to select images from a folder and then display those images in a listview. I tried to modify a sample code,when i run the sample the images are not getting displayed,i have posted the code here,can anybody tell me what i am doing wrong?
public partial class MainWindow : Window
        public MainWindow()
            MyImages = new ObservableCollection<MyImageClass>();
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            ofd.Filter = "Image Files (*.jpg, *.bmp,*.png,*.gif)|*.jpg;*.bmp;*.png;*.gif|All Files|*.*";
            if (ofd.ShowDialog() == true)
                string[] filePath = ofd.FileNames;
                int i=filePath.Length;
                string[] safeFilePath = ofd.SafeFileNames;
                foreach (string imgPath in filePath)
                    foreach (string filename in safeFilePath)
                        MyImages.Add(new MyImageClass(filename, GetImageFromResourceString(imgPath)));
            //MyImages.Add(new MyImageClass("earth", GetImageFromResourceString("earth")));
            //MyImages.Add(new MyImageClass("mercury", GetImageFromResourceString("mercury")));
            //MyImages.Add(new MyImageClass("venus", GetImageFromResourceString("venus")));
        public ObservableCollection<MyImageClass> MyImages { get; set; }
        private BitmapImage GetImageFromResourceString(string imageName)
            BitmapImage image = new BitmapImage();
                    image.UriSource = new Uri(imageName);
            return image; 

    public class MyImageClass
        public MyImageClass(string title, ImageSource image)
            this.Title = title;
            this.Image = image;
        public string Title { get; set; }
        public ImageSource Image { get; set; }

Here is the xaml
<Window x:Class="WpfApplication3.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <Style TargetType="ListBox">
            <Setter Property="ItemsPanel">
                        <WrapPanel />
        <DataTemplate x:Key="MyImagesItemTemplate">
            <Grid Width="100" Height="100">
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                <Image Grid.Row="0" Source="{Binding Path=Image}" />
                <Label Grid.Row="1" Content="{Binding Path=Title}" />
    <Grid >
        <ListBox ItemsSource="{Binding Path=MyImages, ElementName=window1}" ItemTemplate="{StaticResource MyImagesItemTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
Posted 10-Jun-12 0:03am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I think the XAML won't notice that your ObservableCollection has been assigned.

You should initialize the ObservableCollection before you call InitializeComponent() in your constructor. The best thing to do is to move the InitializeComponent() call to the end of the constructor; then the whole collection is already filled.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 25 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100