Search code examples
c#wpfreactive-programmingreactiveui

How to make the ReactiveUI Select when property changed in specific conditions


I'm using ReactiveUI in WPF and I have a Listbox which bind to SearchResults

Now I want the SearchResults change when any of the two properties has changed, so I did this

_searchResults = this.WhenAnyValue(x => x.SearchTerm, x => x.SelectedItem.Deleted)
                .Select(item => item)
                .Select(item => SearchProjects(item.Item1))
                .ObserveOn(RxApp.MainThreadScheduler)
                .ToProperty(this, x => x.SearchResults);

But I want to add some additional conditions to this.

I want to select value only when the the Deleted turn from false to true.

Can someone tell me what should I do, thank you!!!

The complete process is:

I have a listbox with datasource, and the datasource row has property Deleted. After I clicked a delete button of SelectedItem, I do something with database, and refresh the listbox datasource by change the SelectedItem.Deleted to true. The whole process is the same as I want.

But in the end, when I select another Item of the listbox, It refreshes because the Deleted changed to false. I must select the item again because of this refresh.

So I want it to trigger only when Deleted changed from false to true


Solution

  • Just for someone in need, I soluted it by:

    _searchResults = this.WhenAnyValue(x => x.SearchTerm)
                    .CombineLatest(this.WhenAnyValue(x => x.SelectedItem.Deleted)
                                       .Buffer(2, skip: 1)
                                       .Where(item => item[1] == true))
                    .Select(item => item.First)
                    .Select(item => SearchProjects(item))
                    .ObserveOn(RxApp.MainThreadScheduler)
                    .ToProperty(this, x => x.SearchResults);
    
    SelectedItem.Deleted = true;
    

    or

    _searchResults = this.WhenAnyValue(x => x.SearchTerm)
                    .CombineLatest(this.WhenAnyValue(x => x.SelectedItem.Deleted)
                                       .Buffer(2))
                    .Select(item => item.First)
                    .Select(item => SearchProjects(item))
                    .ObserveOn(RxApp.MainThreadScheduler)
                    .ToProperty(this, x => x.SearchResults);
    
    SelectedItem.Deleted = true;
    

    The SelectedItem.Deleted = true; is for trigger it once, so as to be able to observe subsequent changes.