Results 1 to 2 of 2

Thread: Put one graphic into a DataGrid

  1. #1
    Junior Member
    Join Date
    Apr 10
    Posts
    28

    Put one graphic into a DataGrid

    I have a DataGrid in a Silverlight project, fed by an ObservableCollection, and I'd like to have in one column an entry for each row that is either a particular graphic (same graphic for each item), or none. I could use a similar-sized all-white graphic instead if that would be easier than a blank.

    Searching on the internet I've found discussions of feeding graphics to the DataGrid from a SQL database and other similarly complex operations, but that's all far, far more complexity than I'm aiming for. I'd only need the one (or two) small graphic files, and each cell in that column of the grid has either the graphic or nothing. (Or it has the one graphic or the other, if that's a better way to go.)

    Can somebody point me to a discussion of how to do this sort of thing in Silverlight?

    Many thanks!

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,198

    Re: Put one graphic into a DataGrid

    Is the image coming from a Byte() property or will it be a relative resource in your project?

    I'm assuming you're using a TemplateColumn with an Image control in the DataGrid.

    Assuming you're providing a byte array to the converter here's how you'd set up the converter to return a BitmapImage. This also returns a generic N/A image from the images folder if the value you pass is Nothing.

    Code:
    Public Class ImageConverter
        Implements IValueConverter
        Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object _
        Implements IValueConverter.Convert
            If value IsNot Nothing Then
                Dim bi As New Imaging.BitmapImage()
                bi.SetSource(New IO.MemoryStream(DirectCast(value, [Byte]())))
                Return bi
            Else
                Return New Imaging.BitmapImage(New Uri("../images/ImageNotAvailable.png", UriKind.Relative))
            End If
        End Function
    
        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object _
        Implements System.Windows.Data.IValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
    End Class
    Here's the msdn link for IValueConverter for further reading: http://msdn.microsoft.com/en-us/libr...8VS.95%29.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •