locked
How to dynamically adding images to the grid view?

    Question

  • I want to dynamically add images to grid, associating each image with the item in the list.

        public class Offline
        {
            public List<Offline1> lin;
            public Offline()
            {
                lin=new List<Offline1>();
                lin.Add(new Offline1("Number1","image1.jpg"));
                lin.Add(new Offline1("Number2","image2.jpg"));
            }
    
        }
        public class Offline1
        {
            public string Title { get; set; }
            public Imagesource Image { get; set; }
           
            public Offline1(string t, string i)
            {
                Title = t;
                Image = i;
            }
        }

    now when i execute the code i am able to see the two grids Number1 and Number2 but with NO images please help me out.

    Thank you,


    Friday, March 28, 2014 1:54 PM

Answers

  • You set your binding source to offlinefiles, but offlinefiles has both Title (string) and Image(ImageResource). If you want to show the image, then when you display the image you must link it to the images.

    Can you show us your XAML binding to the Grid with the images? That is most likely where your problem is located.Also, did you ensure that IsGrouped=false for your collectionviewsource (I don't recall if the default is false)?

    Did you set your DataContext to:

    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"

    Just things to look into :)

    Saturday, March 29, 2014 6:58 AM

All replies

  • Hi.

    There are 2 ways to do that:

    1. using Binding 

    2. Direct setting your list to gridview.ItemsSource.

    Also, I recommend you to use ObservableCollection<Offline1> instead using List<>. Because when you add item to ObservableCollection - it will show in grid.

    Friday, March 28, 2014 2:41 PM
  • Can you please elaborate.

    yeah i have binded the list to the collection source as follows

    <CollectionViewSource x:Name="itemsOfflineFiles" Source="{Binding offlinefiles}"/>

    and then in the code i'm doing the following thing

     Offline odf = new Offline();
                this.DefaultViewModel["offlinefiles"]=odf.lin;

    and still i'm not able to get the image in the grid.

    Friday, March 28, 2014 3:02 PM
  • You set your binding source to offlinefiles, but offlinefiles has both Title (string) and Image(ImageResource). If you want to show the image, then when you display the image you must link it to the images.

    Can you show us your XAML binding to the Grid with the images? That is most likely where your problem is located.Also, did you ensure that IsGrouped=false for your collectionviewsource (I don't recall if the default is false)?

    Did you set your DataContext to:

    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"

    Just things to look into :)

    Saturday, March 29, 2014 6:58 AM