Bind an Image to a property in WPF

The situation is the following: I have a class that has a Status property of type SingleAnalysisStatus enum, and I want to show a different png image, based on the status of the object.

The solution is to write a custom IValueConverter that convert from the enum to a valid resource file, but we need to pay specific attention. In WPF you can include images as resources in a very simple way, just include the images in the project and set the “build Action” to Resource, as shown in Figure 1.


Figure 1: include an image file as resource in a WPF application

In this way you can simply use this syntax to assign a resource image to an object of type Image

   1: <Image 

   2:     HorizontalAlignment="Left" 

   3:     Height="100" 

   4:     Width="100" 

   5:     Source="/Images/NotMatch.png"/>

You can simply specify the path of the image in the Source property of an Image Element, but to show a different image depending on the value of an enum requires a specific ValueConverter, and you need to be aware that this converter need to convert from the original type to a BitmapImage object, because the Source property of an <image> will not accepts string during binding. Here is how you can accomplish this.

   1: <Window.Resources>

   2:    <Converter:SingleAnalysisStatusConverter x:Key="statusconverter" />

   3:    <DataTemplate x:Key="ItemTemplate">

   4:      <DockPanel> 

   5:         <Image HorizontalAlignment="Right" Height="24" Margin="0"

   6:                       Source="{Binding Status, Converter={StaticResource statusconverter}}"

   7:                       VerticalAlignment="Bottom" Width="24" Stretch="Fill" />

In this snippet of code I’m showing how to bind the Source property of an Image to the Status property of the underling ViewModel, and thanks to the SingleAnalysisStatusConverter object I’m able to convert the status to a valid BitmapImage object. This is the full code of the IValueConverter object.

   1: class SingleAnalysisStatusConverter: IValueConverter 

   2: {


   4:     #region IValueConverter Members


   6:     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

   7:     {

   8:         if (!(value is SingleAnalysisStatus))

   9:             throw new NotImplementedException("SingleAnalysisStatusConverter can only convert from SingleAnalysisStatus");


  11:         String path = null;

  12:         switch ((SingleAnalysisStatus ) value)

  13:             {

  14:                 case SingleAnalysisStatus.NotAnalyzed:

  15:                     path = "Images/NotAnalyzed.png";

  16:                     break;

  17:                 case SingleAnalysisStatus.DuringAnalysis:

  18:                     path = "Images/DuringAnalysis.png";

  19:                     break;

  20:                 case SingleAnalysisStatus.Match:

  21:                     path = "Images/Match.png";

  22:                     break;

  23:                 case SingleAnalysisStatus.NotMatch:

  24:                     path = "Images/NotMatch.png";

  25:                     break;

  26:                 case SingleAnalysisStatus.ErrorDownload:

  27:                     path = "Images/DownloadError.png";

  28:                     break;

  29:             default :

  30:                     throw new NotSupportedException();

  31:             }

  32:         return new BitmapImage(new Uri("/AssemblyName;component/" + path, UriKind.Relative));

  33:     }


  35:     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

  36:     {

  37:         throw new NotImplementedException();

  38:     }


  40:     #endregion


  42: }

The key part is the line

return new BitmapImage(new Uri("/AssemblyName;component/" + path, UriKind.Relative));

that creates the BitmapImage passing an uri composed by the: assemblyname + semicolon + component/ + imagepath. With this simple converter I’m able to show different images based on content of a specific property.


Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

Leave a Reply

Your email address will not be published.