Display items as images in a WPF ListView

So, I bound List to ListView , where List has elements of type Album , where it has many properties, including .Cover , which I am an image on disk. Well, since I don’t know what type of image is needed and how to load it (I only know the use of image types for Winforms), I don’t know the type yet.

Can someone show or send me a quick sample where it shows that such objects are displayed as images of a certain fixed size using their .Cover property?

In essence, this would show:

  • What type of .Cover should be
  • How to open disk images for WPF (provided that it differs from loading Winforms images)
  • How to show them in ListView as images of a certain fixed size , scaling images if necessary
+1
source share
2 answers
  • ImageSource
  • ImageSource myImageSource = new BitmapImage (new Uri file (@ ": // C: ... something.jpg"));
  • Specify the data template for the items of the ListView ItemTemplate property:

     <Window.Resources> <DataTemplate x:Key="ItemTemplate"> <StackPanel Orientation="Horizontal"> <Image Width="10" Height="10" Stretch="Fill" Source="{Binding Cover}"/> <Label Content="{Binding Title}" /> </StackPanel> </DataTemplate> </Window.Resources> <Grid x:Name="grid"> <ListView ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding Albums}" /> </Grid> 
+6
source

In xaml, you must define the DataTemplate in your Listview ItemTemplate , which uses Image , binding it to the Source property on the path in your sysem file.

In other words, Cover can be of type string, the path to the file. If you want to scale, a fairly simple way is to have a ViewBox that scales everything it contains. However, the image itself probably has the ability to do scaling.

+2
source

All Articles