当图像不可见时,将图像源设置为null

时间:2014-08-01 15:04:48

标签: wpf xaml mvvm

我在我的应用中显示图片。我从C#代码中设置了图像源。

首先我设置图像的可见性:

<Setter TargetName="IncludeIcon" Property="Visibility" Value="{Binding Path=Clicked, Converter={StaticResource BooleanToVisibilityConverter}}" />

然后我设置了源:

<Setter TargetName="IncludeIcon" Property="Source" Value="{Binding MyImageSource}"></Setter>

我遇到的问题是 - 当Clicked为假时,我不想要显示图像。这工作正常,但这是我在Visual Studio的输出选项卡中的内容:

    System.Windows.Data Error: 6 : 'TargetDefaultValueConverter' converter failed to convert value '' (type 'String'); fallback value will be used, if available. BindingExpression:Path=MyImageSource; DataItem='MyModel' (HashCode=62805747); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource') NotSupportedException:'System.NotSupportedException: ImageSourceConverter cannot convert from System.String.
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
   at MS.Internal.Data.TargetDefaultValueConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'

1 个答案:

答案 0 :(得分:0)

该错误可能在属性具有其数据绑定值之前发生。更改Visibility属性不会产生它。试试这个:

<Setter TargetName="IncludeIcon" Property="Source" Value="{Binding MyImageSource, 
    IsAsync=True}" />