Browse Source

Don't throw when binding produces invalid value.

pull/237/head
Steven Kirk 11 years ago
parent
commit
ed4f46a6cf
  1. 26
      src/Perspex.Base/PriorityValue.cs
  2. 9
      tests/Perspex.Base.UnitTests/PerspexObjectTests_Binding.cs

26
src/Perspex.Base/PriorityValue.cs

@ -212,25 +212,23 @@ namespace Perspex
/// <param name="priority">The priority level that the value came from.</param>
private void UpdateValue(object value, int priority)
{
if (!TypeUtilities.TryCast(_valueType, value, out value))
if (TypeUtilities.TryCast(_valueType, value, out value))
{
throw new InvalidOperationException(string.Format(
"Invalid value for Property '{0}': {1} ({2})",
_name,
value,
value?.GetType().FullName ?? "(null)"));
}
var old = _value;
var old = _value;
if (_validate != null)
{
value = _validate(value);
}
if (_validate != null)
ValuePriority = priority;
_value = value;
_changed.OnNext(Tuple.Create(old, _value));
}
else
{
value = _validate(value);
// TODO: Log error.
}
ValuePriority = priority;
_value = value;
_changed.OnNext(Tuple.Create(old, _value));
}
/// <summary>

9
tests/Perspex.Base.UnitTests/PerspexObjectTests_Binding.cs

@ -59,14 +59,11 @@ namespace Perspex.Base.UnitTests
}
[Fact]
public void Bind_Throws_Exception_For_Invalid_Value_Type()
public void Bind_Ignores_Invalid_Value_Type()
{
Class1 target = new Class1();
Assert.Throws<InvalidOperationException>(() =>
{
target.Bind((PerspexProperty)Class1.FooProperty, Observable.Return((object)123));
});
target.Bind((PerspexProperty)Class1.FooProperty, Observable.Return((object)123));
Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
}
[Fact]

Loading…
Cancel
Save