protected IDisposable SetupControlEnabling(IPropertyManagerPageControl control, IObservable <bool> enabledObservable) { return(AfterActivationObs .Select(_ => enabledObservable) .Switch() .Subscribe(v => control.Enabled = v)); }
protected IDisposable CreateComboBox <T, TModel>( IPropertyManagerPageGroup @group, string caption, string tip, ICompositeList <T> items, IEqualityComparer <T> itemEqualityComparer, Func <T, string> itemToStringFn, TModel model, Expression <Func <TModel, T> > selectedItemExpression, Action <IPropertyManagerPageCombobox> config = null) { var id = NextId(); var comboBox = @group.CreateComboBox(id, caption, tip); config?.Invoke(comboBox); // Sync source collection with SW ComboBox collection Action <short, T> insert = (index, element) => { var insertIndex = comboBox.InsertItem(index, itemToStringFn(element)); Debug.Assert(insertIndex != -1, "Item couldn't be inserted"); }; Action <short> delete = index => { var deleteIndex = comboBox.DeleteItem(index); Debug.Assert(deleteIndex != -1, "Item couldn't be deleted"); }; var swComboBoxUpdated = new Subject <Unit>(); var d0 = AfterActivationObs .Select(_ => items.ChangesObservable(itemEqualityComparer)) .Switch() .Subscribe(changes => { short index = 0; foreach (var change in changes) { switch (change.Operation) { case DiffOperation.Match: break; case DiffOperation.Insert: insert(index++, change.ElementFromCollection2.Value); break; case DiffOperation.Delete: delete(index); break; case DiffOperation.Replace: delete(index); insert(index++, change.ElementFromCollection2.Value); break; case DiffOperation.Modify: break; default: throw new ArgumentOutOfRangeException(); } } swComboBoxUpdated.OnNext(Unit.Default); }); // Sync source to SW selection var d1 = swComboBoxUpdated .Select(_ => model.WhenAnyValue(selectedItemExpression)) .Switch() .Select(selectedItem => items .Items .Select(list => (short)list.IndexOf(selectedItem)) ) .Switch() .AssignTo(comboBox, box => box.CurrentSelection); // Sync SW to source selection var selectedItemProxy = selectedItemExpression.GetProxy(model); var d2 = ComboBoxSelectionObservable(id) .Select(index => items .Items .Select(list => list[index]) ) .Switch() .AssignTo(selectedItemProxy, p => p.Value); return(ControlHolder.Create(@group, comboBox, d0, d1, d2, swComboBoxUpdated)); }