Cannot set Image Source from IValueConverter?

I'm trying to use an IValueConverter to pick an image to show. In this case, I have images of country flags that I want to show next to the country code. I also have a tooltip with the full country name.

Here is the Xaml I'm using. This Xaml shows a hard-coded image name that works and another image whose source is set from the IValueConverter. The tooltip is also set via an IValueConverter and that works great:

 <ControlTemplate x:Key="CountryCodeWithFlagTemplate">
  <StackPanel Orientation="Horizontal"
    ToolTip="{TemplateBinding Property=ContentControl.Content,
      Converter={StaticResource CountryTooltipConverter}}
">
    <Image VerticalAlignment="Center" Margin="0,0,5,0" Width="16" Height="16"
      Stretch="None" Visibility="Visible" Name="flagWorks"
      Source="Images\Flags\us.png" />
    <Image VerticalAlignment="Center" Margin="0,0,5,0" Width="16" Height="16"
      Stretch="None" Visibility="Visible" Name="flagFails"
      Source="{TemplateBinding Property=ContentControl.Content,
        Converter={StaticResource CountryImageConverter}}"
/>
    <TextBlock Text="{TemplateBinding Property=ContentControl.Content,
      Converter={StaticResource CountryImageConverter}}"
/>
    <ContentPresenter VerticalAlignment="Center"
      Content="{TemplateBinding Property=ContentControl.Content}" />
  </StackPanel>
</ControlTemplate>

The Image in green works great. However, the Image in red does not. The 16x16 space is occupied, but no image is produced. I added the TextBlock in blue just to verify what the CountryImageConverter is returning. It returns the string shown in the green Image that works (in this case, hard-coded to return "Images\Flags\us.png"). I don't know if it makes any difference, but the images are stored in the application's resources.

Also, I'm able to successfully use an IValueConverter to set the Tooltip.

Is there some reason why I can't set the Image Source with the IValueConverter Should I be doing something different



Answer this question

Cannot set Image Source from IValueConverter?

  • Guy Ronen

    I think it's because Image.Source property expects an object of type ImageSource and you provide a string. For the image in green, it works because there is a default conversion from string to ImageSource but for the image in red you have to provide the conversion yourself.
    So the Convert method will look like:
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.UriSource = new Uri(value as string);
    image.EndInit();

    return image;
    }


  • WarNov

    Hi,

    I suspect that this will be a binding or type issue and that the image is being rendered before the convertor is being run or that the convertor is returning a type that the source property does'nt like (if thats the case then the exception is not getting throw up to the top by the image control which is no good). First steps I would experiment with the type first if nothing works there then your looking at a binding issue and these can be a pain to fix. What you may need to do is to add each control to the panel in the CS and set each up that way, it will give you more control over setting up one but is not the nicest way of doing things (but a trade off to get things working).

    HTH

    Andy


  • JBaynton

    Thank you. That was indeed the problem. I remember there was a recent post about how we feel about the typless nature of Xaml. I guess this is a place where it bit me :(

    Just thought I'd add a little additional information in case anyone else has this problem:

    I am setting the images from resources. In order to get a resource image from a Uri, you need to use the following format:

    String.Format( @"pack://application:,,/Images/Flags/{0}.png", imageName );

    The flag images are in the Images\Flags folder in my application. I figure out which image I want and pass it to the string format above and set the result to the value of the Uri. Works great!


  • Cannot set Image Source from IValueConverter?