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;
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue