tags:

views:

10

answers:

1

Hi,

I've got an Enum called "DoYouKnow", containing Yes, No and Unknown.

I have a column in a GridView which currently displays "Yes", "No" or "Unknown" in each row, based on a DoYouKnow value I've attached it to using DisplayMemberBinding.

Instead of displaying I would like to have this column display a tick, a cross or a question mark.

So far the closest I've gotten to getting this working is drawing some pictures and lamenting the fact that if I didn't have to use WPF, I could do this in code without having to bother all you nice people.

Any idea what I should do next?

EDIT: Based on Reed's advice, I have gotten to this stage with my XAML (most of the details have been ripped out, apologies if anything obvious is missing, feel free to ask, for all I know something important IS missing):

<UserControl.Resources>
  <l:KnownImageConverter x:Key="imageConverter"/>
</UserControl.Resources>
<Grid>
  <ListView>
    <ListView.View>
      <GridView>
        <GridViewColumn Header="Known State" DisplayMemberBinding="{Binding Path=Known, Converter={StaticResource imageConverter}}" />
      </GridView>
    </ListView.View>
  </ListView>
</Grid>

And this is the iValueConverter I've cobbled together:

<ValueConversion(GetType(Known), GetType(Image))>
Public Class KnownImageConverter
    Implements IValueConverter

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Select Case CType(value, Known)
            Case Known.Yes
                Return My.Resources.Yes
            Case Known.No
                Return My.Resources.No
            Case Known.Unknown
                Return My.Resources.Unknown
        End Select
        Return New Image
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        If value Is My.Resources.Yes Then
            Return Known.Yes
        ElseIf value Is My.Resources.No Then
            Return Known.No
        ElseIf value Is My.Resources.Unknown Then
            Return Known.Unknown
        End If
        Return Known.Unknown
    End Function
End Class

As I said in the comments below, the Images I'm using are all displaying in the ListView as "System.Drawing.Bitmap" instead of my pretty little pictures.

+1  A: 

You could make an IValueConverter to convert your enum into the appropriate Image. Then you could just bind the image source, with the value converter, directly to your value.

Reed Copsey
iValueConverter works, but the GridViewColumn is displaying "System.Drawing.Bitmap" for every entry, rather than an actual image.
Frosty840
@Frosty840: Without seeing your XAML, it's tough to know the complete problem - but... You're obviously making a `System.Drawing.Bitmap`, which is a GDI bitmap, not a WPF image. If you make a WPF Image, it will probably work correctly. Try making a BitmapImage instead: http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.aspx
Reed Copsey
Reed: Thanks for your help, I've edited my question to make things clearer.
Frosty840
@Frosty840: THe problem is that your converter is converting to `System.Drawing.Image`. That's not a WPF image. You need to make a `System.Windows.Media.BitmapImage` instead.
Reed Copsey