0

I googled a lot and still not get an answer. The problem is very silly - I have collection of elements, and need to know when any property changed on any element. Not collection itself changed.

Pseudocode:

public class Item : ReactiveObject {
    [ObservableAsProperty]
    public bool PropertyToMonitor { get; }
}

public ReadOnlyObservableCollection<Item> Items;

So, how to get that any item in the Items list got PropertyToMonitor updated in observable manner?

Ugly workaround I use by now is:

Observable.Timer(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1))
          .Subscribe(_ => InvokeAsync(StateHasChanged))

Spend a day and gave up after all.

UPD: resolved with quite straight solution, pseudocode:

public class UnitTest3 : ReactiveObject
{
    private readonly ITestOutputHelper _output;
    public class Item : ReactiveObject
    {
        [Reactive]
        public bool PropertyToMonitor { get; set; }
    }
    private ObservableCollection<Item> Items = new ObservableCollection<Item>();
    public UnitTest3(ITestOutputHelper output)
    {
        this._output = output;
    }
    [Fact]
    public void Test1()
    {
        bool signalled = false;
        var item = new Item { };
        Items.Add(new[] {item});
        Items.Select(s => s.WhenPropertyChanged(p => p.PropertyToMonitor).Select(v => v.Value))
             .Merge()
             .Subscribe(v =>
              {
                  _output.WriteLine("signalled: {0}", v);
                  signalled = v;
              });
        item.PropertyToMonitor = true;
        signalled.Should().BeTrue();
    }
}
  • 1
    You can simply implement the `INotifyPropertyChanged` and listen to all your items It will even tell you which object and which property triggered. see [this link](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=net-5.0) for more details – Franck Nov 20 '20 at 16:01
  • @Franck That is what the ObservableAsProperty Attribute actually does. – Ralf Nov 20 '20 at 16:12
  • Maybe this https://stackoverflow.com/questions/55810911/rx-observableasproperty-bound-properties-are-not-automatically-updated-on-gui helps – Ralf Nov 20 '20 at 16:17

0 Answers0