SuperJMN
SuperJMN

Reputation: 13972

Equivalence in ReactiveUI?

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:

Upvotes: 0

Views: 166

Answers (1)

ds-b
ds-b

Reputation: 361

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; }
    }

Upvotes: 4

Related Questions