0

I have this property inside a ReactiveObject:

bool IsValid => Children.All(child => child.IsValid);

The problem is that, of course, it doesn't raise any change notification when children are modified (their "IsValid" property).

How is this done the right way in ReactiveUI?

NOTE:

  • Child is a ReactiveObject, too.
  • I can modify both classes, parent and children, to meet RxUI precepts and guidelines with no restriction.
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • Does `child.IsValid` raise `PropertyChanged` events? – pmbanka Jan 27 '16 at 08:33
  • Child is a ReactiveObject. I can model it freely. How could I propagate changes from the child to the parent? The semantics of what I want to do is: "I'm valid if my children are valid." – SuperJMN Jan 27 '16 at 18:15

1 Answers1

4

ObservableAsPropertyHelper< bool > is what you need, if your Children property is a reactive list you can merge Changed and ItemChanged observables and have something like:

public class MyViewModel : ReactiveObject
    {
        private readonly ObservableAsPropertyHelper<bool> _isValidPropertyHelper;

        public MyViewModel()
        {
            var listChanged = Children.Changed.Select(_ => Unit.Default);
            var childrenChanged = Children.ItemChanged.Select(_ => Unit.Default);
            _isValidPropertyHelper = listChanged.Merge(childrenChanged)
                                                .Select(_ => Children.All(c => c.IsValid))
                                                .ToProperty(this, model => model.IsValid);

        }
        public bool IsValid
        {
            get { return _isValidPropertyHelper.Value; }
        }

        public ReactiveList<Item> Children { get; set; }
    }
ds-b
  • 361
  • 1
  • 5
  • 1
    This assumes that OP wants to update `MyViewModel.IsValid` only when a child is added/removed/replaced, not when `child.IsValid` changes. I'm not sure if that was his intent. – pmbanka Jan 27 '16 at 12:37
  • @ds-b I'm sorry, but I feels your answer doesn't work :( – SuperJMN Jan 27 '16 at 18:10
  • 1
    I think if Children is a `ReactiveList` you will need to set `Children.ChangeTrackingEnabled = true` for this to work. – Grokys Jan 27 '16 at 18:48