【发布时间】:2012-01-21 18:58:09
【问题描述】:
我在 XAML 中有一个 Image 元素。我将 Source 属性绑定到 ViewModel 中的字符串属性。但是,有时该值为 null,然后在调试窗口中出现错误。
我在这里读到:ImageSourceConverter error for Source=null,如果值为 null,我可以制作一个转换器以返回 DependencyProperty.UnsetValue。
现在我想知道是否可以直接在 XAML 中执行此操作?也许通过使用 FallbackValue? 我尝试了一些变体,但没有运气。
这是我在 XAML 中的图像元素:
<Image Name="img" Source="{Binding Path=CurrentImageSource}" Stretch="None" />
CurrentImageSource 只是 DataContext 上的一个字符串属性。
错误信息是: System.Windows.Data 错误:23:
无法将 '' 从类型 '' 转换为 type 'System.Windows.Media.ImageSource' 用于默认的 'sv-SE' 文化 转换;考虑使用 Binding 的 Converter 属性。 NotSupportedException:'System.NotSupportedException: ImageSourceConverter 无法从 (null) 转换。
【问题讨论】: