3

I've problems with IValueconverter and dynamically load a row grid image:

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

    string Type = (string)value;
    Uri uri;
    try
    {
        uri = new Uri("/XConsole;component/Images/" + Type + ".png", UriKind.Relative);
        return new System.Windows.Media.Imaging.BitmapImage(uri) { CacheOption = BitmapCacheOption.None };
    }
    catch (Exception e)
    {
        //donothing
    }
    uri = new Uri("/XConsole;component/Images/Type_SYSTEM.png", UriKind.Relative);
    return new System.Windows.Media.Imaging.BitmapImage(uri) { CacheOption = BitmapCacheOption.None };

}

I want to pass to converter a "Type" of my object and load different images:

 <Image Grid.Column="0"  Width="25" Height="25" Margin="2,0" Source="{Binding Path=Type, Converter={StaticResource imageConverter}}" >

But there is something wrong because no picture are loaded!

if i load statically, it' s ok:

 <Image Grid.Column="0"  Width="25" Height="25" Margin="2,0" Source="/XconsoleTest;component/Images/Type_DB.png" >

i loaded my converter in UserControl.Resources:

<UserControl.Resources>
    <converters:ImageConverter x:Key="imageConverter"/>
</UserControl.Resources>

Help me!!

Kapitán Mlíko
  • 4,498
  • 3
  • 43
  • 60
davymartu
  • 1,393
  • 3
  • 15
  • 34
  • Do you see any binding errors in the VS output window? Also, don't use "Type" as your variable name, both in the converter and in the object you're binding to as it already exists as a class. I don't know if this is what creates the problem though. – siger Feb 13 '13 at 16:34
  • If i debug in my converter, it load correctly value and URI found my image resource, but in UI there is nothing! – davymartu Feb 13 '13 at 16:44

1 Answers1

4

If you create the URI in code you have to write the full Pack URI including the pack://application:,,, part. In XAML this is not necessary as it is automatically added by the string-to-ImageSource TypeConverter.

var type = (string)value;
var uri = new Uri("pack://application:,,,/XConsole;component/Images/" + type + ".png");

Note that the above code sample uses a lowercase type identifier instead of Type to avoid confusion with the Type class.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • I have try... Uri and new BitmapImage(Uri) don't throw FileNotFoundException, but in UI always nothing.... – davymartu Feb 13 '13 at 17:24
  • I've found [link](http://social.msdn.microsoft.com/Forums/en-SG/wpf/thread/10cb5a57-c048-40e8-8f14-032dd00320d6) and i've modified:`uri = new Uri(@"pack://application:,,/Images/Type_DB.png");` and now work ok...but i don't understand why! – davymartu Feb 13 '13 at 17:41
  • 1
    The MSDN article on [Pack URIs in WPF](http://msdn.microsoft.com/en-us/library/aa970069.aspx) explains everything. If the image resources are in the local assembly (same as the converter), you have to drop the `/XConsole;component` part. – Clemens Feb 13 '13 at 18:36