0

I am trying to prevent the user from continuing until they have selected at least one item in a list. The list is a bound to a separate view control inside the main view and the list viewmodel has a bool with 2 way binding to a checkbox in its view. The command to continue is part of the main viewmodel so to try and listen to changes I am creating a derived list

tempList = this.WhenAnyValue(x => x.Items,
(layers) => layers == null ? Enumerable.Empty<bool>() : layers.CreateDerivedCollection(x => x.Selected))
.ToProperty(this, x => x.TempList);

CanContinue = this.WhenAnyValue(m => m.CollectionInfo, m => m.TempList,
(collectionDatabase, tempItems) =>
{
    if (collectionDatabase == null || tempItems == null) return false;

    var temp = tempItems.Any(x => x == true);
    return temp;
});

The problem is that the TempList never changes. I can see the Selected property changing on the child viewmodel but that's it.

I have also tried assigning the TempList as

tempList = Items.Changed.Select(_ => Items.Select(gr => gr.Selected))
.ToProperty(this, x => x.TempList);

Anyone have any suggestions? The data model is out of my control as in the real application I'm loading sets of data from disk that can contain one or more sets of data. I've got a sample app that demonstrates the issue if you want to try it for yourself.

Dave Timmins
  • 331
  • 2
  • 6

1 Answers1

0

I managed to solve it, the important bits are to set ChangeTrackingEnabled = true on the ReactiveList and then subscribe to the ItemChanged notification for the collection, this means that the TempList is no longer needed either. I also changed the predicate for the command to use an int with the number of selected items.

var canContinue = this.WhenAnyValue(x => x.NumSelected, num => num > 0);

this.WhenAnyObservable(x => x.Items.ItemChanged)
    .Subscribe(_ => NumSelected = Items.Count(i => i.Selected));
Ahe
  • 2,124
  • 3
  • 19
  • 29
Dave Timmins
  • 331
  • 2
  • 6
  • 1
    I think that better solution would be transforming NumSelected to output property and instead of Subscribe and assignment use Select and ToProperty. – Ahe Oct 07 '15 at 06:59