简体   繁体   中英

displaying image to listview wpf

what do i need to add in my code below?

what i'm doing is to view the images in the listview

so far this is my code

<ListView Name="Thumbnails">
   <ListView.ItemsPanel>
      <ItemsPanelTemplate>
         <UniformGrid Columns="1"/>
      </ItemsPanelTemplate>
   </ListView.ItemsPanel>
</ListView>


foreach (var item in lstImages)
{
  Thumbnails.Items.Add(new BitmapImage(new Uri(item)));
}

what do i need in order to show the images to the listview. the output of the code is only string, i did a search but i don't understand the code i found. thank you

Set the ItemTemplate property of the ListView (or a ListBox) to a DataTemplate that contains an Image control, and bind the Image control's Source property to the collection element.

<ListView Name="Thumbnails">
     <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="1"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Now add BitmapImage instances to the Items collection of the ListView:

foreach (var item in lstImages)
{
    Thumbnails.Items.Add(new BitmapImage(new Uri(item)));
}

Due to built-in automatic type conversion from string and Uri (and byte[] ) to ImageSource , you could as well just add the image file paths to the Items collection:

foreach (var item in lstImages)
{
    Thumbnails.Items.Add(item);
}

As next step you may want to have a view model with an ObservableCollection<string> property that holds the image file paths. You would bind the ListView's ItemsSource property to this collection.

将一个Image元素添加到ListView中:

Thumbnails.Items.Add(new Image() { Source = new BitmapImage(new Uri("yourpic.png", UriKind.Relative)) });

The ListView's ItemSource should be set to the list of URIs. Then you need to set up an ItemTemplate that contains an image control with the Source bound to the value.

ListView.DataTemplate
    ItemTemplate
        Image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM