mirror of https://github.com/AMT-Cheif/Stylet.git
Fix bug in BoolToVisibilityConverter, causing exception with some argument types
This commit is contained in:
parent
1d9a5d4248
commit
092cf02599
|
@ -53,15 +53,30 @@ namespace Stylet.Xaml
|
||||||
{
|
{
|
||||||
bool result;
|
bool result;
|
||||||
if (value == null)
|
if (value == null)
|
||||||
|
{
|
||||||
result = false;
|
result = false;
|
||||||
|
}
|
||||||
else if (value is bool)
|
else if (value is bool)
|
||||||
|
{
|
||||||
result = (bool)value;
|
result = (bool)value;
|
||||||
|
}
|
||||||
else if (value is IEnumerable)
|
else if (value is IEnumerable)
|
||||||
|
{
|
||||||
result = ((IEnumerable)value).GetEnumerator().MoveNext();
|
result = ((IEnumerable)value).GetEnumerator().MoveNext();
|
||||||
else if (value.Equals(System.Convert.ChangeType((object)0, value.GetType())))
|
}
|
||||||
result = false;
|
|
||||||
else
|
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;
|
return result ? this.TrueVisibility : this.FalseVisibility;
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,6 +129,12 @@ namespace StyletUnitTests
|
||||||
Assert.AreEqual(Visibility.Visible, this.converter.Convert("hello", null, null, null));
|
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]
|
[Test]
|
||||||
public void ConvertBackThrowsIfTargetTypeIsNotBool()
|
public void ConvertBackThrowsIfTargetTypeIsNotBool()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue