AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
24.98k stars 2.17k forks source link

2-Way Binding to IObservable-like object (+XAML support) #16260

Open maxkatz6 opened 1 month ago

maxkatz6 commented 1 month ago

See discussion for more details https://github.com/AvaloniaUI/Avalonia/discussions/16257

Right now, there is no stable API after binding refactoring, that allows creating TwoWay observable bindings, only an obsolete API to be removed in 12. We need to implement a replacement API before that.

Also, we might want to support twoway observable bindings in XAML as well, something that we didn't have before. It should be possible by checking if target property implements both IObserver and IObservable of the same type.

maxkatz6 commented 1 month ago

Current obsolete API that still works:

public class InstancedBinding
{
        public static InstancedBinding TwoWay(
            IObservable<object?> observable,
            IObserver<object?> observer,
            BindingPriority priority = BindingPriority.LocalValue);
}

var instancedBinding = InstancedBinding.TwoWay(/* */);
BindingOperations.Apply(menuItem, MenuItem.IsCheckedProperty, instancedBinding);