我在XAML中有一个Image元素.我将Source属性绑定到ViewModel中的字符串属性.但是,有时值为null,然后我在调试窗口中出现错误.
我在这里读到:Source = null的ImageSourceConverter错误,如果值为null,我可以使转换器返回DependencyProperty.UnsetValue.
现在我想知道是否可以直接在XAML中进行操作?也许通过使用FallbackValue?我尝试了一些变种,但没有运气.
这是我在XAML中的Image元素:
<Image Name="img" Source="{Binding Path=CurrentImageSource}" Stretch="None" />
Run Code Online (Sandbox Code Playgroud)
而CurrentImageSource只是DataContext上的一个字符串属性.
错误消息是:System.Windows.Data错误:23:
无法将''从类型''转换为'sv-SE'文化的'System.Windows.Media.ImageSource',默认转换为; 考虑使用Binding的Converter属性.NotSupportedException:'System.NotSupportedException:ImageSourceConverter无法转换(null).