using System;
using System.Reactive;
namespace Avalonia.Data
{
///
/// Holds a description of a binding for 's [] operator.
///
public class IndexerDescriptor : ObservableBase, IDescription
{
///
/// Gets or sets the binding mode.
///
public BindingMode Mode
{
get;
set;
}
///
/// Gets or sets the binding priority.
///
public BindingPriority Priority
{
get;
set;
}
///
/// Gets or sets the source property.
///
public AvaloniaProperty? Property
{
get;
set;
}
///
/// Gets or sets the source object.
///
public AvaloniaObject? Source
{
get;
set;
}
///
/// Gets or sets the source observable.
///
///
/// If null, then . will be used.
///
public IObservable? SourceObservable
{
get;
set;
}
///
/// Gets a description of the binding.
///
public string Description => $"{Source?.GetType().Name}.{Property?.Name}";
///
/// Makes a two-way binding.
///
/// The current binding.
/// A two-way binding.
public static IndexerDescriptor operator !(IndexerDescriptor binding)
{
return binding.WithMode(BindingMode.TwoWay);
}
///
/// Makes a two-way binding.
///
/// The current binding.
/// A two-way binding.
public static IndexerDescriptor operator ~(IndexerDescriptor binding)
{
return binding.WithMode(BindingMode.TwoWay);
}
///
/// Modifies the binding mode.
///
/// The binding mode.
/// The object that the method was called on.
public IndexerDescriptor WithMode(BindingMode mode)
{
Mode = mode;
return this;
}
///
/// Modifies the binding priority.
///
/// The binding priority.
/// The object that the method was called on.
public IndexerDescriptor WithPriority(BindingPriority priority)
{
Priority = priority;
return this;
}
///
protected override IDisposable SubscribeCore(IObserver observer)
{
if (SourceObservable is null && Source is null)
throw new InvalidOperationException("Cannot subscribe to IndexerDescriptor.");
if (Property is null)
throw new InvalidOperationException("Cannot subscribe to IndexerDescriptor.");
return (SourceObservable ?? Source!.GetObservable(Property)).Subscribe(observer);
}
}
}