I created an IValueConverter
that converts a bool to a System.Windows.Visibility
object (it does the opposite of BooleanToVisibilityConverter
). It works fine, except when I try to use it on an Observable<bool>
object. Observable<T>
is a class I defined with an implicit operator to convert it to a T. The problem is that when unboxing it as a bool, I get an InvalidCastException
.
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value) // InvalidCastException
{
return Visibility.Hidden;
}
else
{
return Visibility.Visible;
}
}
It seems that C# is ignoring my implicit operator when the Observable
is boxed as an Object
. For example:
Observable<bool> obs = new SimpleObservable<bool>(true);
object obj = obs;
// This conversion works just fine:
bool bval = (bool)(Observable<bool>)obj;
// This conversion throws an InvalidCastException:
bval = (bool)obj;
Is there any way to guarantee that my Observable<bool>
can be unboxed as a bool?