Reputation: 587
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!
Upvotes: 0
Views: 673
Reputation: 128097
Your converter should return a value that matches the type of the target property, i.e. an instance of a type derived from ImageSource
. This is typically a BitmapImage
or a BitmapFrame
. In case no image shall be displayed, it should return null
:
public object Convert(
object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
object result = null;
if (value is GrappleType)
{
switch ((GrappleType)value)
{
case GrappleType.Hydraulic:
break;
case GrappleType.Parallel:
result = new BitmapImage(new Uri(
"pack://application:,,,/GUI;component/Images/040/SensorSoft.png"));
break;
}
}
return result;
}
Please note how the converter uses a Resource File Pack URI to create a BitmapImage.
Upvotes: 1