0

i'm trying to apply a converter for my image source binding. here's my xaml:

<Window.Resources>
        <DataTemplate x:Key="listBoxTemplate">
            <StackPanel Orientation="Horizontal">
                <StackPanel.Resources>
                    <ImageConverter x:Key="MyImageConverter" />
                </StackPanel.Resources>
                <Image Source="{Binding Path=thumb, StringFormat=/WpfTest;component/Images/{0}, Converter={StaticResource MyImageConverter}}" Height="100" Width="130" Margin="5"></Image>
                <StackPanel Orientation="Vertical" Width="247">
                    <TextBlock Text="{Binding recipeName}" Height="60" Padding="15" FontSize="16" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBlock>
                    <TextBlock Text="{Binding cuisine}" Height="60" Padding="15" FontSize="16" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBlock>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>

and here's my imageConverter class:

using System;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Media.Imaging;

public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string path = (string)value;

        try
        {
            //ABSOLUTE
            if (path.Length > 0 && path[0] == System.IO.Path.DirectorySeparatorChar
                || path.Length > 1 && path[1] == System.IO.Path.VolumeSeparatorChar)
                return new BitmapImage(new Uri(path));

            //RELATIVE
            return new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + path));
        }
        catch (Exception)
        {
            return new BitmapImage();
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

but when i tried running the apps, it returns "the type imageconverter was not found" and VS highlight the part

<ImageConverter x:Key="MyImageConverter" />

in the xaml above. How do I fix it? (Btw I got the imageconverter code from Wpf - relative image source path )

Emond
  • 50,210
  • 11
  • 84
  • 115
imin
  • 4,504
  • 13
  • 56
  • 103

1 Answers1

2

You need to add its namespace like this:

<ns:ImageConverter x:Key="MyImageConverter"/>

And make sure you added the namespace higher up like this:

<DataTemplate xmlns:ns="....">

The actual namespace depends on you project but code completion will help you.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • sorry, a really wpf and c# noobie here.. this is what I should do, step by step: 1) change to (2) add xmlns:ns="...." to which then becomes – imin May 21 '12 at 17:03
  • one more thing u said "The actual namespace depends on you project but code completion will help you.", there are tons of schemas.microsoft.com bla bla that the code completion shows.. which one should I choose? thanks – imin May 21 '12 at 17:08
  • the one that contains the converter. probably the name of the project as I see no namespace in the code you posted. You should put the converter in a namespace; the global namespace is a bad place to keep your classes. – Emond May 21 '12 at 17:11
  • that does it..thanks a lot @Erno!! been stuck with this problem for a day and now it's solved.. may netherlands win this euro 2012!! – imin May 21 '12 at 17:20