0

I have ListBox in my WPF project that gets set to a datasource of "MyObjectCollection". I have managed to get the ListBox to display my collection, and each item to display two string properties from the object. The object also contains an Image, how do i get the image to display in the ListBox?

I am currently using the below code to bind to my DataSource

<UserControl.Resources>
        <DataTemplate x:Key="CustomerTemplate">
            <Border BorderThickness="2" BorderBrush="silver" CornerRadius="5" Padding="1"
                        HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Grid>
                    <Image Source="{Binding Artwork}" Tag="{Binding Artwork}" VerticalAlignment="Stretch" ></Image>
                        <TextBlock Text="{Binding Name}"  Foreground="#515151"
                       FontSize="16" HorizontalAlignment="Stretch"
                       FontWeight="Bold" />
                    <TextBlock Text="{Binding Length}" Foreground="#515151" Margin="0,25,0,0"
                       FontSize="10" HorizontalAlignment="Stretch"
                       FontWeight="Bold" />
                </Grid>
            </Border>
        </DataTemplate>
</UserControl.Resources>

Thanks, Ben

Ben
  • 3,926
  • 12
  • 54
  • 87
  • 1
    The Image control expects the source to be a URI, not an actual image... Can you tell us what type "Artwork" is? – Brent Apr 26 '10 at 21:26
  • Artwork is an System.Drawing.Image Type – Ben Apr 27 '10 at 07:10
  • `System.Drawing.Image` is WindowsForms while [`System.Windows.Media.ImageSource`](http://msdn.microsoft.com/en-us/library/system.windows.media.imagesource.aspx) is the type that should be used in WPF. As Brent pointed out, the simplest way is to use a `Uri`. – gehho Apr 27 '10 at 07:16
  • The only problem with that is the fact that i am loading the image from a SQL Server Database, and dont really want to have to persist the image to disk in order to display it in the grid. – Ben Apr 27 '10 at 10:43

1 Answers1

0

It depends on the image type in your collection.

If it is a path string to a file or if it is a byte array.

You should use ValueConverter for your image binding.

Take a look at ValueConverter

MSL
  • 990
  • 1
  • 12
  • 28