0

I am having problems with a listview in WPF C#. I have a class called FaultRecords which contains an integer field, a bitmap image and a string. When I run the code the integer and string appear fine but the image does not. In the column where the image is supposed to be I just get the path to the type instead - "system.windows.media.imaging.bitmapimage". Its driving me crazy.....

Here's my code

public void RecordNewFault()
{
    FaultRecords myRecord = new FaultRecords(FaultIndex, PublicVars.SrcBmp, "Hi There!");
    lstvFaults.Items.Add(myRecord);

}

public class FaultRecords
{
    private int _faultNumber;
    public int FaultNumber
    {
        get { return _faultNumber; }
        set { _faultNumber = value; }
    }


    public BitmapImage _faultImage;
    public BitmapImage FaultImage
    {
        get { return _faultImage; }
        set { _faultImage = value; }
    }


    private string _faultDescription;
    public string FaultDescription
    {
        get { return _faultDescription; }
        set { _faultDescription = value; }
    }


    public FaultRecords(int faultNumber, BitmapImage faultImage, string faultDescription) 
    {
        FaultDescription = faultDescription;
        FaultNumber = faultNumber;
        FaultImage = faultImage;
    }
}

XAML:

 <ListView Height="412" HorizontalAlignment="Left" Margin="312,49,0,0"         Name="lstvFaults" VerticalAlignment="Top" Width="636" ItemsSource="{Binding}"    FontSize="12"> 
                              <ListView.View>
                    <GridView>
                            <GridViewColumn 
                                DisplayMemberBinding="{Binding Path=FaultNumber}"
                                Header="Fault No." Width="50"/>
                            <GridViewColumn 
                               DisplayMemberBinding="{Binding Path=FaultImage}"
                                Header="Photo" Width="150"/>
                        <GridViewColumn 
                               DisplayMemberBinding="{Binding Path=FaultDescription}"
                            Header="Fault Description" Width="300"/>
                    </GridView>
                    </ListView.View>

If I just write the Bitmap to the listview on it's own (and remove the XAML bindings) - lstvFaults.Items.Add(PublicVars.SrcBmp); it works fine.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Chief Wiggum
  • 121
  • 1
  • 2
  • 7
  • You have to override the ListView ItemTemplate with a custom template that binds your image to an Image object. – Xcalibur37 Oct 08 '12 at 13:02
  • @Blam I thought I had got it working but I alas have not. I am now using a class to bind to my listview, this works fine. However it does not show the image in the listview it displays the type instead. – Chief Wiggum Oct 08 '12 at 14:43
  • I am talking about your other questions. – paparazzo Oct 08 '12 at 15:23

2 Answers2

1

See this related question.

You need to tell the ListView to render the content not as Text (hence it is showing you the Type), but as an Image.

Community
  • 1
  • 1
pleinolijf
  • 891
  • 12
  • 29
0

You will need to use a DataTemplate. See a simplified example here. Also you will need an ImageSource and not a BitmapImage! Sorry :(

noorbakhsh
  • 43
  • 5
  • Thanks noorbakhsh - I had actually figured this out myself and was coming here to close the question :-), as soon as I changed to ImageSource it worked. – Chief Wiggum Oct 10 '12 at 10:45