diff --git a/Stylet/Xaml/BoolToVisibilityConverter.cs b/Stylet/Xaml/BoolToVisibilityConverter.cs index 466a893..e42f9e4 100644 --- a/Stylet/Xaml/BoolToVisibilityConverter.cs +++ b/Stylet/Xaml/BoolToVisibilityConverter.cs @@ -53,15 +53,30 @@ namespace Stylet.Xaml { bool result; if (value == null) + { result = false; + } else if (value is bool) + { result = (bool)value; + } else if (value is IEnumerable) + { result = ((IEnumerable)value).GetEnumerator().MoveNext(); - else if (value.Equals(System.Convert.ChangeType((object)0, value.GetType()))) - result = false; + } else - result = true; // Not null, didn't meet any other falsy behaviour + { + // This fails if it an int can't be converter to it, or for many other reasons + // Easiest is just to try it and see + try + { + result = !value.Equals(System.Convert.ChangeType((object)0, value.GetType())); + } + catch + { + result = true; // Not null, didn't meet any other falsy behaviour + } + } return result ? this.TrueVisibility : this.FalseVisibility; } diff --git a/StyletUnitTests/BoolToVisibilityConverterTests.cs b/StyletUnitTests/BoolToVisibilityConverterTests.cs index 6de8f4b..1f3de23 100644 --- a/StyletUnitTests/BoolToVisibilityConverterTests.cs +++ b/StyletUnitTests/BoolToVisibilityConverterTests.cs @@ -129,6 +129,12 @@ namespace StyletUnitTests Assert.AreEqual(Visibility.Visible, this.converter.Convert("hello", null, null, null)); } + [Test] + public void ConvertTreatsRandomObjectAsTrue() + { + Assert.AreEqual(Visibility.Visible, this.converter.Convert(typeof(int), null, null, null)); + } + [Test] public void ConvertBackThrowsIfTargetTypeIsNotBool() {