Example #1
0
        public StructureHolderViewModel(TProperty property, ViewModelFactory factory)
            : base(property)
        {
            PropertyClosedUp = new ReactiveProperty <IPropertyViewModel>();
            Members          = property.Members.Select(x => factory.Create(x, true)).ToArray();
            OnChanged        = Observable.Merge(Members.Select(x => x.OnChanged));
            EditCommand.Subscribe(x => ShowDetailSubject.OnNext(this));

            foreach (var member in Members)
            {
                member.ShowDetail.Subscribe(x => PropertyClosedUp.Value = x);
            }
        }
        public SubtypingViewModel(SubtypingProperty property, ViewModelFactory factory)
            : base(property)
        {
            Instance = Property.Model.Where(x => x != null)
                       .Select(x => factory.Create(x, true))
                       .ToReactiveProperty();

            EditCommand = property.Model.Select(x => x != null)
                          .ToReactiveCommand();

            EditCommand.Where(x => Instance.Value != null).Subscribe(x =>
            {
                ShowDetailSubject.OnNext(Instance.Value);
            });
            Instance.Subscribe(x => ShowDetailSubject.OnNext(null));

            OnChanged = Instance.Where(x => x != null)
                        .SelectMany(x => x.OnChanged)
                        .Merge(Instance.Select(x => Unit.Default));
        }
        public CollectionViewModel(TProperty property, ViewModelFactory factory) : base(property)
        {
            Collection = property.Collection.ToReadOnlyReactiveCollection(x =>
            {
                IPropertyViewModel vm = null;
                try
                {
                    vm = factory.Create(x);
                }
                catch (Exception e)
                {
                    OnErrorSubject.OnNext(e);
                    return(null);
                }
                vm.OnChanged.Subscribe(y => OnChangedSubject.OnNext(Unit.Default));
                vm.OnError.Subscribe(e => OnErrorSubject.OnNext(e));
                return(vm);
            });

            FormatedString = Property.Count.Select(x => $"Count = {x}")
                             .ToReactiveProperty(mode: ReactivePropertyMode.RaiseLatestValueOnSubscribe);

            AddCommand.Subscribe(x =>
            {
                try
                {
                    Property.AddNewElement();
                }
                catch (Exception e)
                {
                    OnErrorSubject.OnNext(e);
                }
                OnChangedSubject.OnNext(Unit.Default);
            });
            RemoveCommand.Subscribe(x =>
            {
                try
                {
                    Property.RemoveElementAt(x);
                }
                catch (Exception e)
                {
                    OnErrorSubject.OnNext(e);
                }
                OnChangedSubject.OnNext(Unit.Default);
            });
            EditCommand.Subscribe(x => ShowDetailSubject.OnNext(this));

            UpCommand.Subscribe(x =>
            {
                Property.Move(x - 1, x);
                SelectedIndex.Value = x - 1;
            });
            DownCommand.Subscribe(x =>
            {
                Property.Move(x + 1, x);
                SelectedIndex.Value = x;
            });

            DuplicateCommand.Subscribe(x =>
            {
                Property.Duplicate(x, x + 1);
            });
        }