views:

686

answers:

2

This is killing me, I can't get an image to display as a list box item: here is my code:

WPF:

 // listbox called lstWidgets
<ListBox.ItemTemplate>
 <DataTemplate>
    <StackPanel>
      <TextBlock Name="txtTitle" Margin="2,5,5,2" Text="{Binding name}" />
      <Image Name="imgDisp"  Source="{Binding img}" Width="50" Height="50"/>
   .....

C#:

Class widget / props: string name, Image img (get,set)...
ObservableCollection<cls_Widget> widgets....
Image newImage = new Image();
newImage.Source = new ImageSourceConverter().ConvertFromString("")as ImageSource;
cls_Widget wdg = new cls_Widget();  
wdg.img = newImage
wdg.name = "My Widget";
widgets.Add(wdg);                  
lstWidgets.ItemsSource = widgets;
....

The textblock Text is displayed, but the image is not (the image area is blank).. I appreciate any help! I have gotten and image to display in different code scenarios, but not this one...

Thanks in advance.

+2  A: 

I think you need to expose the imageSource, not an Image. You already have an Image in the template.

Looking at the debug output in Visual Studio might indicate what is failing on the binding, by the way.

Denis Troller
Ahh ye old output window (showed me the binding error).. I changed the class prop to an image source, and it worked.. Mucho Gracias
jdr120
Why is this not the accepted answer
benPearce
A: 

If you are binding the Source of the image to a backing property, the backing property should be an image source, not an image.

Alternatively, you could use a ContentControl to display the image in your object. Try this:

...
<TextBlock Name="txtTitle" Margin="2,5,5,2" Text="{Binding name}" />
<ContentControl Name="imgDisp" Width="50" Height="50" Padding="0"
                Content="{Binding img}"/>
...
Josh G