我试图从我的服务器检索图像以在我的WPF应用程序中显示。 所有图像都已命名为使用它们所代表的对象的名称。
例如:电影名称=重力 链接:http://www.somesite.com/something/something/Gravity.jpg
我试图通过绑定对象名称来污染图像文件夹的默认路径。
尝试这个没有用(甚至给我错误)
<Image Height="100" Width="100"
Source="http://www.something.com/something/something/"
+ {Binding Name}
+ ".jpg"/>
你能建议一个更好的办法吗?
答案 0 :(得分:3)
您可以使用转换器。 ConverterParameter
用于指定BaseURI,绑定值将包含图像名称。
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();
}
}
然后在xaml:
<Image Source="{Binding ImageName, Converter={StaticResource Converter, ConverterParameter='http://www.something.com/something/something/'}}"/>
现在我在转换器中对.jpg
进行了硬编码,如果您希望将其作为变量,可以将其添加为参数。