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

2 Answers 2

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));
}
Sign up to request clarification or add additional context in comments.

6 Comments

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
the return in your answer is not of type ImageSource ..right ?
BitmapImage implements class BitmapSource which eventually derives from ImageSource. Hence the return type is of ImageSource.
ok, i tried it, iam getting an exception:Invalid URI: The format of the URI could not be determined.
Set mode to be Relative for an URI. Refer to this link for the reason - stackoverflow.com/questions/9734120/…
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.