views:

202

answers:

3

So I have binded a List to a ListView where the List has items of type Album, where it has lots of properties including .Cover, which I is an image on disk. Well since I don't know what type of image is needed and how they have to be loaded (I only know using Image types for Winforms), I don't know the type yet.

Can someone show or post me a quick sample where it shows this sort of items being shown as images of a certain fixed size using their .Cover property?

In essence this would show:

  1. What type .Cover should be
  2. How to open images from disk for WPF (assuming it's different than Winforms image loading)
  3. How to show them on a ListView as images of a certain fixed size, scaling the images if necessary
+1  A: 

In xaml you'd define a DataTemplate in your Listview's ItemTemplate that uses an Image, binding it's Source property to a path on your file sysem.

In other words, Cover can be of type string, a file path. If you want to scale, a pretty simple way is a ViewBox, which scales all it contains. However, Image itself probably has options to do scaling.

flq
Thanks, can you please show me code sample. I don't know ViewBox or the Image binding.
Joan Venge
+2  A: 
  1. ImageSource
  2. ImageSource myImageSource = new BitmapImage(new Uri(@"file://C:... something.jpg"));
  3. Specify a data template for the items in the ListView's 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>
    
Scott J
Note that the image can also be sourced from a resource, it doesn't have to come directly from disk. (The resource can be part of the current executing assembly, or it can be in a separate referenced assembly). It can even be a URL to a web address.
slugster
Thanks slugster how do you do that?
Joan Venge
A: 

Also getting xaml parsing error from this, anyone knows why, I really don't.

<Window x:Class="Cool.CoolWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CoolWindow"
        Height="300"
        Width="300">

<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>
Joan Venge
What's the full text of the error? Does it reference a line number?
Scott J
It says it's the first line in the xaml. But now I narrowed it down to this I think. Basically I am having a trouble having a constructor of Album that takes a image path for the cover and sets the Cover as ImageSource like you did.
Joan Venge
SO I am gathering the list of images as file paths and passing them to this: http://utilitybase.com/paste/28837
Joan Venge
I updated the Title from the xaml you posted but still.
Joan Venge
Is that just a mis-copy, or is your contstructor named incorrectly? "Film" instead of "Album"?
Scott J
Yeah I copied it wrong, sorry about that. It was gonna be Album.
Joan Venge
But was I setting .Cover correctly to an ImageSource. I never used ImageSource and this Uri thing seems weird to use, since I am only familiar with Winforms way of doing this.
Joan Venge
Your code is working for me, so the only thing I can think of is to check the string you're sending for the uri. The URI is just like a URI you would specify in a browser. If you wanted to grab an image off the web, it would be "http://...". Make sure the path you send is correct and there's a file there. (I just tried deliberately sending a bad path and I got a XAML parse error, so it's likely that's the problem.)
Scott J
Thanks, now it works. But I see nothing even though I added to a public List<Album> Albums property 3 items. Do you know why this might be?
Joan Venge
Ah, I bet it's the thing that get's me every time. In the Window constructor, after InitializeComponent, add DataContext = this;
Scott J
Incidentally, whenever there's a binding error, the exception is caught and the message written to the "Output" window. If you look, you'll likely see databinding errors for all your bindings before you add that line.
Scott J
Thanks Scott now I can see the Titles but the Covers don't show up. They are just empty so there is space for them. And for exception I get this:A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in PresentationCore.dll
Joan Venge
And I am loading the images from C:\ so it's very simple path and the jpg files are there.
Joan Venge
What's the inner most message of any nested excptions?Possibility: what type of images are they? WPF supports jpg, bmp, gif, png, tiff, and ico.
Scott J
They are jpg. But the exception is only printed to the output, doesn't prevent the window to come up. Do they have be case sensitive or maybe no spaces?
Joan Venge
spaces should be fine and it's not case sensitive. What's the error that gets printed in the Output? It might also be possible that another app has a lock on the image file.
Scott J
Only this exception in the output:A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in PresentationCore.dllThe thread 0x2a3c has exited with code 0 (0x0).In your version your images look fine?I don't think the images are locked. I can delete them manually easily which I did to see.
Joan Venge
But images are 256x256, that's not a problem?
Joan Venge
I'm using a sample image that came with Windows. My path is @"C:\Users\Public\Pictures\Sample Pictures\penguins.jpg". That image is 1024x768. Do you get the exception if you comment out the image in the template? (just to make sure.)
Scott J
Sorry just found it, it was the Cover property that was commented out when I was narrowing down the crash. Sorry for wasting your time. But last question, if you want the items shown like in windows explorer where the items are side by side that flow by the width of the window? Would it be a WrapPanel?
Joan Venge
By that I also meant having the label at the bottom of the image. But if it's too much, I can ask a separate question.
Joan Venge
It looks like it's WrapPanel but since the Title is show to the right of the items, they aren't flown with a small width. So I just need to get the text show up at the bottom of the Cover.
Joan Venge
Here's an example of how to use a WrapPanel with a ListView http://blogs.msdn.com/atc_avalon_team/archive/2006/04/28/585844.aspx. You would modify the DataTemplate in the style for ListViewItem to include the Title.
Scott J
Thanks Scott, appreciate your help!
Joan Venge