jdr120
jdr120

Reputation: 23

WPF Listbox Image binding question (again)!

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.

Upvotes: 1

Views: 3637

Answers (2)

Josh G
Josh G

Reputation: 14256

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}"/>
...

Upvotes: 0

Denis Troller
Denis Troller

Reputation: 7501

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.

Upvotes: 3

Related Questions