我正在try 将自定义对象列表绑定到WPF图像,如下所示:

<Image>
    <Image.Source>
        <BitmapImage UriSource="{Binding Path=ImagePath}" />
    </Image.Source>
</Image>

但这不管用.这是我收到的错误:

"Property 'UriSource' or property 'StreamSource' must be set."

我错过了什么?

推荐答案

WPF具有特定类型的内置转换器.如果将图像的所以urce属性绑定到stringUri值,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}}"/>

.net相关问答推荐

如何使用 awslocal 通过 localstack 中的 cloudwatch events/eventbridge 触发 lambda

如何在 C# 中将 HTTP 发布请求发送到另一个 API?

AsyncLocal 的语义与逻辑调用上下文有何不同?

即时窗口中的动态导致Microsoft.CSharp.RuntimeBinder.Binder未定义或导入错误

如何使用 C# 关键字作为属性名称?

比较 C# 中的双精度值

.NET 的 `Array.Sort()` 方法使用的排序算法是稳定的算法吗?

无法加载文件或程序集WebGrease,版本=1.5.1.25624,Culture=neutral,PublicKeyToken=31bf3856ad364e35或其依赖项之一

为什么 Interlocked.Exchange 不支持布尔类型?

C#:获得完整的桌面大小?

覆盖方法上的 C# 可选参数

修剪数组中的所有字符串

是否有可用的 WPF 备忘单?

mscorlib 代表什么?

如何将 WebResponse.GetResponseStream 返回转换为字符串?

一个接口是否应该继承另一个接口

如何仅在需要时提升权限?

并发字典正确用法

如何判断对象是否已在 C# 中释放

如何卸载Microsoft .NET Core 1.0.0 RC2 - VS 2015 Tooling Preview 1?