using System.Reactive; using System.Reactive.Subjects; using Avalonia.Data; namespace Avalonia.ReactiveUI; public static class AvaloniaObjectReactiveExtensions { /// /// Gets a subject for an . /// /// The object. /// The property. /// /// The priority with which binding values are written to the object. /// /// /// An which can be used for two-way binding to/from the /// property. /// public static ISubject GetSubject( this AvaloniaObject o, AvaloniaProperty property, BindingPriority priority = BindingPriority.LocalValue) { return Subject.Create( Observer.Create(x => o.SetValue(property, x, priority)), o.GetObservable(property)); } /// /// Gets a subject for an . /// /// The property type. /// The object. /// The property. /// /// The priority with which binding values are written to the object. /// /// /// An which can be used for two-way binding to/from the /// property. /// public static ISubject GetSubject( this AvaloniaObject o, AvaloniaProperty property, BindingPriority priority = BindingPriority.LocalValue) { return Subject.Create( Observer.Create(x => o.SetValue(property, x, priority)), o.GetObservable(property)); } /// /// Gets a subject for a . /// /// The object. /// The property. /// /// The priority with which binding values are written to the object. /// /// /// An which can be used for two-way binding to/from the /// property. /// public static ISubject> GetBindingSubject( this AvaloniaObject o, AvaloniaProperty property, BindingPriority priority = BindingPriority.LocalValue) { return Subject.Create>( Observer.Create>(x => { if (x.HasValue) { o.SetValue(property, x.Value, priority); } }), o.GetBindingObservable(property)); } /// /// Gets a subject for a . /// /// The property type. /// The object. /// The property. /// /// The priority with which binding values are written to the object. /// /// /// An which can be used for two-way binding to/from the /// property. /// public static ISubject> GetBindingSubject( this AvaloniaObject o, AvaloniaProperty property, BindingPriority priority = BindingPriority.LocalValue) { return Subject.Create>( Observer.Create>(x => { if (x.HasValue) { o.SetValue(property, x.Value, priority); } }), o.GetBindingObservable(property)); } }