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.

4

2 回答 2

1

请参阅此相关问题

您需要告诉 ListView 将内容呈现为不是文本(因此它向您显示类型),而是呈现为图像。

于 2012-10-08T11:53:07.197 回答
0

您将需要使用 DataTemplate。请参阅此处的简化示例。您还需要一个 ImageSource 而不是 BitmapImage!对不起 :(

于 2012-10-08T16:27:26.837 回答