我正在try 将自定义对象列表绑定到WPF图像,如下所示:
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding Path=ImagePath}" />
</Image.Source>
</Image>
但这不管用.这是我收到的错误:
"Property 'UriSource' or property 'StreamSource' must be set."个
我错过了什么?
我正在try 将自定义对象列表绑定到WPF图像,如下所示:
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding Path=ImagePath}" />
</Image.Source>
</Image>
但这不管用.这是我收到的错误:
"Property 'UriSource' or property 'StreamSource' must be set."个
我错过了什么?
WPF具有特定类型的内置转换器.如果将图像的所以urce
属性绑定到string
或Uri
值,under the hood WPF将使用Image所以urceConverter将该值转换为Image所以urce
.
所以
<Image 所以urce="{Binding Image所以urce}"/>
would work if the Image所以urce property was a string representation of a valid URI to an image.
当然,您可以使用自己的绑定转换器:
public class ImageConverter : IValueConverter
{
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
return new BitmapImage(new Uri(value.ToString()));
}
public object ConvertBack(
object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
这样使用:
<Image 所以urce="{Binding Image所以urce, Converter={StaticResource ImageConverter}}"/>