C# 向图像源添加绑定

C# 向图像源添加绑定,c#,wpf,image,binding,C#,Wpf,Image,Binding,我正在尝试从我的服务器检索一个要在WPF应用程序中显示的图像。 所有图像都已命名为使用它们所表示的对象的名称 例如:电影名称=重力 链接: 我试图用对象名称的绑定污染图像文件夹的默认路径 尝试这样做不起作用(甚至给了我错误) 您能建议一种更好的方法吗?您可以使用转换器ConverterParameter用于指定BaseURI,绑定值将包含图像名称 public class ImagePathConverter : IValueConverter { public object Con

我正在尝试从我的服务器检索一个要在WPF应用程序中显示的图像。 所有图像都已命名为使用它们所表示的对象的名称

例如:电影名称=重力 链接:

我试图用对象名称的绑定污染图像文件夹的默认路径

尝试这样做不起作用(甚至给了我错误)



您能建议一种更好的方法吗?

您可以使用转换器
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/'}}"/>


现在,我在converter中硬编码了
.jpg
,如果希望它成为变量,可以将其添加为参数。

可以使用转换器
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/'}}"/>


现在,我在converter中硬编码了
.jpg
,如果希望它成为变量,可以将其添加为参数。

可以使用转换器
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/'}}"/>


现在,我在converter中硬编码了
.jpg
,如果希望它成为变量,可以将其添加为参数。

可以使用转换器
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/'}}"/>


现在,我在converter中硬编码了
.jpg
,如果希望它成为变量,可以将其作为参数添加。

+1。也许还值得展示如何通过绑定的
ConverterParameter
属性将格式字符串传递给转换器。这可能有助于OP在XAML中指定格式。或者,它可以是转换器的一个属性,当它被实例化为resource.+1时设置。也许还值得展示如何通过绑定的
ConverterParameter
属性将格式字符串传递给转换器。这可能有助于OP在XAML中指定格式。或者,它可以是转换器的一个属性,当它被实例化为resource.+1时设置。也许还值得展示如何通过绑定的
ConverterParameter
属性将格式字符串传递给转换器。这可能有助于OP在XAML中指定格式。或者,它可以是转换器的一个属性,当它被实例化为resource.+1时设置。也许还值得展示如何通过绑定的
ConverterParameter
属性将格式字符串传递给转换器。这可能有助于OP在XAML中指定格式。或者,它可以是转换器的一个属性,当它被实例化为资源时设置。