图片UriSource和数据绑定 [英] Image UriSource and Data Binding
问题描述
我想自定义对象的列表绑定到一个WPF图像是这样的:
I'm trying to bind a list of custom objects to a WPF Image like this:
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding Path=ImagePath}" />
</Image.Source>
</Image>
不过,这是行不通的。这是我收到的错误:
But it doesn't work. This is the error I'm getting:
属性UriSource'或财产'StreamSource的'必须设置。的
我在想什么?
推荐答案
的BitmapImage UriSource是甲流,我想,没有一个内置的转换器吧。
BitmapImage UriSource is a stream, i think, and there isn't a built in converter for it.
WPF有内置转换器某些共同的绑定。如果您绑定图像的源为一个字符串值,引擎盖下的WPF将使用值转换器的字符串,用于转换为URI,并获得BitmapImage的。
WPF has built in converters for certain common bindings. If you bind the Image's Source to a string value, underneath the hood WPF will use a value converter to convert the string to a URI, and get the BitmapImage from that.
所以,如果不是你这样做:
So if instead you did this:
<Image Source="{Binding ImageSource}" />
它的工作(如果ImageSource的财产是一个字符串,再一个有效的URI presentation到图像)
It would work (if the ImageSource property was a string representation of a valid uri to an image)
当然你也可以推出自己的,并在Silverlight你需要,因为问题的Image控件与绑定:
You can of course roll your own, and in Silverlight you need to because of issues the Image control has with Bindings:
public sealed class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
try
{
return new BitmapImage(new Uri((string)value));
}
catch
{
return new BitmapImage();
}
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这篇关于图片UriSource和数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!