0

In my project, i have a folder called Images, where all the images iam using in my application are saved in subfolders.All the images are set to "Resource" in the buildprocess.

myproject
  |__Images
      |__AppImages
          |__StarOn.png
          |__StarOff.png

Now, if i do set my image manually like this:

<Image Source="Images\AppImages\StarOn.png" width="32" height="32"/>

the image is correctly shown in the imagebox.

i would like to set the image using a converter and a binding like this:

<Image>
<Image.Source>
  <Binding Path="Number" converter="{StaticResource GetImagePathConverter}"/>
</Image.Source>
</Image>

where the number is an integer

and my converter is:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int questionNr=int.parse(value.ToString());

            if (questionNr>100)
            {
                return "Images\\AppImages\\StarOn.png";
            }

            return "Images\\AppImages\\starOff.png";
    }

but this is not changing the image ?..

what iam doing wrong ? how can i set the image source correctly from the converter ?

thanks in advance

lebhero
  • 1,391
  • 5
  • 18
  • 35

2 Answers2

4

Your way of using converter is incorrect. You need to create an instance of your converter use it in your binding through StaticResource. local: is the local namespace which you need to declare in your xaml -

<Image>
  <Image.Resources>
    <local:GetImagePathConverter x:Key="GetImagePathConverter"/>
  </Image.Resources>
  <Image.Source>
    <Binding Path="Number" Converter="{StaticResource GetImagePathConverter}"/>
  </Image.Source>
</Image>

Also, Source property is not of type string but instead ImageSource so you need something in your converter -

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
   int questionNr=int.parse(value.ToString());

   if (questionNr>100)
   {
      return new BitmapImage(new Uri("Images\\AppImages\\StarOn.png", UriKind.Relative));
   }
   return new BitmapImage(new Uri("Images\\AppImages\\StarOff.png", UriKind.Relative));
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • hi ...thank you for your answer..iam using it correctly in my code..i just forgot to write it here ...thanks again i will edit my question – lebhero Sep 02 '12 at 18:40
  • the return in your answer is not of type ImageSource ..right ? – lebhero Sep 02 '12 at 18:46
  • BitmapImage implements class BitmapSource which eventually derives from ImageSource. Hence the return type is of `ImageSource`. – Rohit Vats Sep 02 '12 at 18:49
  • ok, i tried it, iam getting an exception:Invalid URI: The format of the URI could not be determined. – lebhero Sep 02 '12 at 18:50
  • Set mode to be `Relative` for an URI. Refer to this link for the reason - http://stackoverflow.com/questions/9734120/cannot-set-image-source-in-code-behind – Rohit Vats Sep 02 '12 at 18:57
  • JA ..exactly thanks new BitmapImage(new Uri(@"Images\AppImages\starOn.png",UriKind.Relative)); – lebhero Sep 02 '12 at 18:57
0

See this answer.

Basically, you have to take care of the type of object that you return in your converter, you cannot return string to a property of ImageSource type.

I'm not on my dev machine, but the code is something like this:

return new BitmapImage(new Uri(the/path/to/image.png)).Source; //or '*.ImageSource', can't remember
Community
  • 1
  • 1
XAMeLi
  • 6,189
  • 2
  • 22
  • 29