I have this variable binding in my View:
<Image Source="{Binding Path=GrappleTypeVar, Source={StaticResource CustomerData}, Converter={StaticResource GrappleDataConverter}}" Width="40" Height="40"/>
And then, this converter:
public class GrappleDataConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
int currentType = (int)value;
if (Enum.IsDefined(typeof(GrappleType), currentType))
{
switch ((GrappleType)currentType)
{
case GrappleType.Hydraulic:
return String.Empty;
case GrappleType.Parallel:
return "/GUI;component/Images/040/SensorSoft.png";
}
}
}
// Not defined... Set unknown image
return String.Empty;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
Using that code, my result windows returned a lot of binding errors of the type:
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=GrappleTypeVar; DataItem='CustomerData' (HashCode=50504364); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource') System.Windows.Data Error: 23 : Cannot convert '' from type 'String' to type 'System.Windows.Media.ImageSource' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: ImageSourceConverter cannot convert from System.String.
Reviewing that error, I found that solution: ImageSourceConverter error for Source=null
And I changed in my code:
case GrappleType.Hydraulic:
return String.Empty;
for
case GrappleType.Hydraulic:
return DependencyProperty.UnsetValue;
Now the application runs smoother, but in the result windows it appears the following binding error: System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=GrappleTypeVar; DataItem='CustomerData' (HashCode=62171008); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')
Can anyone provide me some help? Is it possible to solve this error?
Thanks!
Image.Source
is an abstract typeImageSource
(which cannot be instantiated). In your converter, instead of a string, you should return a derived class object such asBitmapImage
. – Peter