Pregunta

I'm trying to retrieve an image from my server to show in my WPF application. All the images have been named to use the name of the object they represent.

For example: Movie name = Gravity Link : http://www.somesite.com/something/something/Gravity.jpg

I'm trying to contaminate the default path of the image folder with the binding of the name of the object.

Trying this didn't work (and even gave me errors)

<Image Height="100" Width="100" 
       Source="http://www.something.com/something/something/" 
               + {Binding Name} 
               + ".jpg"/>

Could you suggest a better way this could be done?

¿Fue útil?

Solución

You can use a converter. ConverterParameter is used to specify BaseURI and binding value will contain image name.

public class ImagePathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
         return string.Format("{0}{1}.jpg",parameter, value);
    }

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

Then in xaml:

<Image Source="{Binding ImageName, Converter={StaticResource Converter, ConverterParameter='http://www.something.com/something/something/'}}"/>

For now I hard coded .jpg in converter, you can add it as a parameter if you want it to be a variable.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top