我在WPF用户控件中显示带有Web URL的图像时遇到问题.我已经解决了2008年8月在本网站上提出的类似问题的所有建议(图像UriSource和数据绑定),但这些建议都没有奏效.
我想做的是:
<Image Width="50" Name="MemberImage">
<Image.Source>
<BitmapImage DecodePixelWidth="50" UriSource="{Binding Member.ImageFilePathUri}" />
</Image.Source>
</Image>
Run Code Online (Sandbox Code Playgroud)
ImageFilePathUri是一个从字符串路径创建的Uri:
public Uri ImageFilePathUri
{
get
{
return new Uri(this.ImageFilePath);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这样就必须设置"Property'UriSource'或属性'StreamSource'." 错误如预期.
我也尝试过使用值转换器:
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var image = new BitmapImage();
image.BeginInit();
if (value != null)
{
image.UriSource = new Uri((string)value);
}
image.DecodePixelWidth = 50;
image.EndInit();
return image;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,使用以下方法绑定它:
<Image Name="TestImage" Width="50" Source="{Binding Path=Member.ImageFilePath, Converter=Parliament.HansardApplicationSuite.Logging.Helpers.ImageConverter}"></Image> …Run Code Online (Sandbox Code Playgroud)