1

我的 silverlight 项目中有一个BooleanToVisibilityConverter和一个InverseBooleanToVisibilityConverter。两者都相当标准,但由于某种原因,在这个对话框中它们抛出了很多错误(至少我认为它是转换器)。我似乎无法让它抛出任何我可以查看的异常我只是得到这个输出,我不知道为什么!

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). 

这是 XAML

<converters:InverseBooleanToVisibilityConverter x:Key="InverseBoolToVisibility" />
<Grid Visibility="{Binding CategoryTitle, Converter={StaticResource InverseBoolToVisibility}, FallbackValue=True}">

我绑定的财产:

public bool CategoryTitle { get; set; }

和转换器本身

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;
    }
}
4

1 回答 1

2
System.ArgumentException: Requested value 'True' was not found.

这是因为Visibility期望一个Visibility Enum类型。您正试图boolFallBackValue.

于 2013-11-04T14:25:20.413 回答