0

I used the method described in the following question to populate a combo box.

ComboBox ItemTemplate does not support values of type 'Image'

The ComboBox is populated with instances of a wrapper class that contains the Image and some data. However as the poster of that question stated the images do not display in the combobox, however selecting one of the blank entries does bind the data correctly.

The data is bound in the model view as this interacts with the external data source, however the images are UI specific so are bound in the code of the user control.

How can I get the images displaying?

code:

public ObservableCollection<Wrapper> Images { get; set; }
public Class Wrapper
{
    public Wrapper(int data,  Image img)
    {
            Data = data;
            Img = img;              
    }
    public int Data { get; set; }
    public Image Img { get; set; }

}

int selectedData;

public int SelectedData
{
    get { return selectedData; }
    set
        {
            selectedData = value;
            (this.Resources["model"] as MyModel).External = Images[selectedData].Data;
            SendPropertyChanged("SelectedData");
        }
 }

xaml:

<ComboBox ItemsSource="{Binding Images}" SelectedIndex="{Binding SelectedData}">
     <ComboBox.ItemTemplate>
         <DataTemplate>
             <Image Source="{Binding Img}" Height="83" Width="71"/>
         </DataTemplate>
     </ComboBox.ItemTemplate>
</ComboBox>
Community
  • 1
  • 1
DanBrum
  • 419
  • 1
  • 7
  • 18

1 Answers1

1

The Source of an Image control can not be another Image control. Change the type of your Img property to ImageSource:

public class Wrapper
{
    ...
    public ImageSource Img { get; set; }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • You can also use `string` and give the path to the image. To create an `ImageSource` you can use the `BitmapImage` class. – RandomEngy Dec 18 '12 at 15:40
  • I was creating an image source with BitmapFrame and assigning that to the Bound Image. Setting the bound property to be the source as you described has worked like a charm. – DanBrum Dec 18 '12 at 15:58