I have a IValueConverter that converts "null" to "Visible":
public class InverseNullToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (targetType != typeof(System.Windows.Visibility))
throw new InvalidOperationException("The target must be a Visibility");
if (value == null)
return Visibility.Visible;
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
This worked fine in Visual Studio 2019, however when i updated to 2022 i got error on every place that used this valueconverter. The first if-case ALWAYS returns true, no matter what input is given.
Why does not "if (targetType != typeof(System.Windows.Visibility))" work in VS 2022? What should i change to make it work?
Im using .Net Framework 4.7.2.
The error message that i receive is XDG0066, it has no text other than the text i provide in the throw "The target must be a Visibility"
An example of where i used the valueconverter:
<Rectangle x:Name="Back" Fill="{TemplateBinding Background}" Visibility="{TemplateBinding ImageBack, Converter={StaticResource InverseNullToVisibilityConverter}}"/>
CodePudding user response:
It is a problem in the XAML designer, which seems to apply your converter in a context where the target type is not exactly Visibility
.
Change your check to a more generally applicable expression, like
if (!targetType.IsAssignableFrom(typeof(Visibility)))
{
throw new InvalidOperationException("The target must be assignable from Visibility");
}