Browse Source

Subscribe to ExpressionSubject before setting value.

The inner `ExpressionObserver` must be subscribed to as setting the
value requires the expression to be evaluated.
pull/691/head
Steven Kirk 10 years ago
parent
commit
daff817ce4
  1. 4
      src/Markup/Avalonia.Markup/Data/ExpressionObserver.cs
  2. 85
      src/Markup/Avalonia.Markup/Data/ExpressionSubject.cs

4
src/Markup/Avalonia.Markup/Data/ExpressionObserver.cs

@ -123,7 +123,9 @@ namespace Avalonia.Markup.Data
/// <param name="priority">The binding priority to use.</param> /// <param name="priority">The binding priority to use.</param>
/// <returns> /// <returns>
/// True if the value could be set; false if the expression does not evaluate to a /// True if the value could be set; false if the expression does not evaluate to a
/// property. /// property. Note that the <see cref="ExpressionObserver"/> must be subscribed to
/// before setting the target value can work, as setting the value requires the
/// expression to be evaluated.
/// </returns> /// </returns>
public bool SetValue(object value, BindingPriority priority = BindingPriority.LocalValue) public bool SetValue(object value, BindingPriority priority = BindingPriority.LocalValue)
{ {

85
src/Markup/Avalonia.Markup/Data/ExpressionSubject.cs

@ -111,56 +111,59 @@ namespace Avalonia.Markup.Data
/// <inheritdoc/> /// <inheritdoc/>
public void OnNext(object value) public void OnNext(object value)
{ {
var type = _inner.ResultType; using (_inner.Subscribe(_ => { }))
if (type != null)
{ {
var converted = Converter.ConvertBack( var type = _inner.ResultType;
value,
type,
ConverterParameter,
CultureInfo.CurrentUICulture);
if (converted == AvaloniaProperty.UnsetValue) if (type != null)
{
converted = TypeUtilities.Default(type);
_inner.SetValue(converted, _priority);
}
else if (converted is BindingNotification)
{ {
var error = converted as BindingNotification; var converted = Converter.ConvertBack(
value,
type,
ConverterParameter,
CultureInfo.CurrentUICulture);
Logger.Error( if (converted == AvaloniaProperty.UnsetValue)
LogArea.Binding,
this,
"Error binding to {Expression}: {Message}",
_inner.Expression,
error.Error.Message);
if (_fallbackValue != AvaloniaProperty.UnsetValue)
{ {
if (TypeUtilities.TryConvert( converted = TypeUtilities.Default(type);
type, _inner.SetValue(converted, _priority);
_fallbackValue, }
CultureInfo.InvariantCulture, else if (converted is BindingNotification)
out converted)) {
{ var error = converted as BindingNotification;
_inner.SetValue(converted, _priority);
} Logger.Error(
else LogArea.Binding,
this,
"Error binding to {Expression}: {Message}",
_inner.Expression,
error.Error.Message);
if (_fallbackValue != AvaloniaProperty.UnsetValue)
{ {
Logger.Error( if (TypeUtilities.TryConvert(
LogArea.Binding, type,
this,
"Could not convert FallbackValue {FallbackValue} to {Type}",
_fallbackValue, _fallbackValue,
type); CultureInfo.InvariantCulture,
out converted))
{
_inner.SetValue(converted, _priority);
}
else
{
Logger.Error(
LogArea.Binding,
this,
"Could not convert FallbackValue {FallbackValue} to {Type}",
_fallbackValue,
type);
}
} }
} }
} else
else {
{ _inner.SetValue(converted, _priority);
_inner.SetValue(converted, _priority); }
} }
} }
} }

Loading…
Cancel
Save