// Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; using Avalonia.Data; namespace Avalonia.Data.Core.Plugins { /// /// Base class for data validators. /// /// /// Data validators are s that are returned from an /// . They wrap an inner /// and convert any values received from the inner property accessor into /// s. /// public abstract class DataValidatiorBase : PropertyAccessorBase, IObserver { private readonly IPropertyAccessor _inner; /// /// Initializes a new instance of the class. /// /// The inner property accessor. protected DataValidatiorBase(IPropertyAccessor inner) { _inner = inner; } /// public override Type PropertyType => _inner.PropertyType; /// public override object Value => _inner.Value; /// public override bool SetValue(object value, BindingPriority priority) => _inner.SetValue(value, priority); /// /// Should never be called: the inner should never notify /// completion. /// void IObserver.OnCompleted() { } /// /// Should never be called: the inner should never notify /// an error. /// void IObserver.OnError(Exception error) { } /// /// Called when the inner notifies with a new value. /// /// The value. void IObserver.OnNext(object value) => InnerValueChanged(value); /// /// Begins listening to the inner . /// protected override void SubscribeCore() => _inner.Subscribe(InnerValueChanged); /// protected override void UnsubscribeCore() => _inner.Dispose(); /// /// Called when the inner notifies with a new value. /// /// The value. /// /// Notifies the observer that the value has changed. The value will be wrapped in a /// if it is not already a binding notification. /// protected virtual void InnerValueChanged(object value) { var notification = value as BindingNotification ?? new BindingNotification(value); PublishValue(notification); } } }