A converter such as what follows will cause the 2008 visual studio designer to not display the xaml, and error out with a "Specified cast is not valid." exception.
public class ItemsVisibilityToGridColumnWidthConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//THE TWO OFFENDING LINES...
var itemsVisibility = (Visibility)values[0];
var orientation = (Orientation)values[1];
if (orientation == Orientation.Horizontal && itemsVisibility != Visibility.Visible)
{
return new GridLength(0);
}
return new GridLength(4, GridUnitType.Star);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Changing the cast to use a method such as this fixes the issue:
static class EnumCaster
{
internal static Orientation CastAsOrientation(object value)
{
if (value is Enum)
{
return (Orientation)value;
}
return Orientation.Horizontal;
}
internal static Visibility CastAsVisibility(object value)
{
if (value is Enum)
{
return (Visibility)value;
}
return Visibility.Visible;
}
}
My question is, wtf is wrong with the Visual Studio designer? And, is there a better way to cast these objects to their corresponding Enum in such a way that the designer doesn't throw a fit?
I think it might happen because at some point, the converter is called with bad arguments. You can debug the call the converter in the designer by following these steps :
That way you should be able to examine the arguments passed to the converter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With