Search code examples
c#windows-phonelonglistselector

C# Windows Phone: Is it possible to fill LongListSelector with images?


I fill a LongListSelector with names of products and i would like to put images of products too in my LongListSelector. I get my datas from my webserver using webclient method. To get an image i only know i should use something like this:

pic.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://srvname.com/images/greenpasta.jpg"));

But i don't know how to show images on long list selector.


Solution

  • You should keep the URL of the image as an attribute to your product, not the image source itself. So you can have something like

        myProduct.uri = new Uri("http://srvname.com/images/greenpasta.jpg")
    

    And in xaml:

        <DataTemplate>
        <Grid>
          <Grid.ColumnDefinition>
          <ColumnDefinition Witdh="100" \>
          <ColumnDefinition Witdh="Auto" \>
          <ColumnDefinition Witdh="*" \>
          </Grid.ColumnDefinition>
          <Image Source="{Binding uri}" Height="100" Stretch="Fill"/> >           
          <TextBlock Text="{Binding Name}" /> >
        </Grid>
        </DataTemplate>
    

    Just as a suggestion, by replacing the stackpanel to a grid, you can already reserve space for your image (100 pixels), which is downloaded async. Otherwise, when the image will apear on the screen, it will move all the other content.