System.ArgumentException: Requested value 'True' was not found.
This is because Visibility
expects a Visibility Enum
type. You are attempting to use a bool
value for this in your FallBackValue
.
题
I have a BooleanToVisibilityConverter
and an InverseBooleanToVisibilityConverter
in my silverlight project. Both fairly standard but for some reason in this one dialog they throw lots of errors (at least I think its the Converter). I cannot seem to get it to throw any exceptions I can view I just get this output and I'm not sure why!
System.Windows.Data Error: 'MS.Internal.Data.DynamicValueConverter' converter failed to convert value 'True' (type 'System.String'); BindingExpression: Path='CategoryTitle' DataItem='null' (HashCode=0); target element is 'System.Windows.Controls.Grid' (Name=''); target property is 'Visibility' (type 'System.Windows.Visibility').. System.ArgumentException: Requested value 'True' was not found.
at System.Enum.EnumResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument)
at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
at MS.Internal.SilverlightTypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, CultureInfo culture, Boolean isForward)
at MS.Internal.Data.TargetDefaultValueConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture)
at MS.Internal.Data.DynamicValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
at System.Windows.Data.BindingExpression.ConvertValue(Object value, DependencyProperty dp, String failureResource).
Here is the XAML
<converters:InverseBooleanToVisibilityConverter x:Key="InverseBoolToVisibility" />
<Grid Visibility="{Binding CategoryTitle, Converter={StaticResource InverseBoolToVisibility}, FallbackValue=True}">
The property Im bound to:
public bool CategoryTitle { get; set; }
And the converter itself
public class InverseBooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !(bool)value ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Visibility vis = (Visibility)value;
if (vis != Visibility.Visible)
{
return true;
}
return false;
}
}
解决方案
System.ArgumentException: Requested value 'True' was not found.
This is because Visibility
expects a Visibility Enum
type. You are attempting to use a bool
value for this in your FallBackValue
.