public static ViewModel.InteractiveCollectionViewModel <T, object> Build <T>(Func <T, object> getkey, IObservable <IEnumerable <T> > elems, IObservable <IFilter> filter, IObservable <T> DeletedSubject, IObservable <object> ClearedSubject, System.Reactive.Concurrency.DispatcherScheduler UI, IObservable <Func <T, object> > getkeys = null, bool isReadOnly = false)
        {
            var dx = Observable.Create <string>(_ => () => { });

            ViewModel.InteractiveCollectionViewModel <T, object> interactivecollection = null;
            ISubject <Exception> exs = new Subject <Exception>();
            var sx = ObservableChangeSet.Create(cache =>
            {
                var dels = DeletedSubject /*.WithLatestFrom(RemoveSubject.StartWith(Remove).DistinctUntilChanged(), (d, r) => new { d, r })*/.Subscribe(_ =>
                {
                    try
                    {
                        cache.Remove(_);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("error removing " + _.ToString() + " from cache");
                        Console.WriteLine(ex.Message);
                        exs.OnNext(ex);
                        //ArgumentNullException
                    }
                });

                ClearedSubject.Subscribe(_ => cache.Clear());

                elems.Subscribe(_ =>
                {
                    foreach (var g in _)
                    {
                        try
                        {
                            cache.AddOrUpdate(g);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("error adding " + g.ToString() + " from cache");
                            Console.WriteLine(ex);
                            exs.OnNext(ex);
                        }
                    }
                });
                return(new System.Reactive.Disposables.CompositeDisposable(dels));
            }, getkey)
                     .Filter(filter.Select(_ => { Func <T, bool> f = aa => _.Filter(aa); return(f); }).StartWith(ft));

            getkeys?.Subscribe(_ =>
            {
                sx.ChangeKey(_);
            });

            interactivecollection = new ViewModel.InteractiveCollectionViewModel <T, object>(sx, UI, DeletedSubject, _ => (IConvertible)getkey(_));

            exs.Subscribe(ex =>
                          (interactivecollection.Errors as System.Reactive.Subjects.ISubject <Exception>).OnNext(ex));

            return(interactivecollection);
        }
Exemplo n.º 2
0
        private void Init()
        {
            var key = KeySubject.DistinctUntilChanged().Merge(ItemsSourceSubject.Take(1)
                                                              .Select(_ => GetKey(_))
                                                              .Where(_ => _ != null)).DistinctUntilChanged();

            Build(ItemsSourceSubject.Select(v => v.Cast <object>()), key).Subscribe(a_ =>
            {
                this.Dispatcher.InvokeAsync(() =>
                {
                    interactivecollection = a_;
                    CollectionChanged();
                },
                                            System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            });
        }
Exemplo n.º 3
0
        public ListBoxEx(Func <object, object> _keyfunc)
        {
            Uri resourceLocater = new Uri("/UtilityWpf.View;component/Themes/ListBoxEx.xaml", System.UriKind.Relative);
            ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);

            Style = resourceDictionary["ListBoxExStyle"] as Style;

            UI = new System.Reactive.Concurrency.DispatcherScheduler(Application.Current.Dispatcher);

            //_.GetType().GetProperty("Object").GetValue(_)
            interactivecollection = ViewModel.InteractiveCollectionFactory.Build(
                _ => _keyfunc(_),//UtilityHelper.PropertyHelper.GetPropValue<object>(_,"Object")),
                ItemsSourceSubject.Select(v => v.Cast <object>()),
                FilterSubject,
                DeletedSubject.WithLatestFrom(RemoveSubject.StartWith(Remove).DistinctUntilChanged(), (d, r) => r ? d : null).Where(v => v != null),
                ClearedSubject,
                UI, null, IsReadOnly
                );
            CollectionChanged();

            Init();
        }
Exemplo n.º 4
0
        public ListBoxEx()
        {
            Uri resourceLocater = new Uri("/UtilityWpf.View;component/Themes/ListBoxEx.xaml", System.UriKind.Relative);
            ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);

            Style = resourceDictionary["ListBoxExStyle"] as Style;

            UI = new System.Reactive.Concurrency.DispatcherScheduler(Application.Current.Dispatcher);
            if (Key != null)
            {
                interactivecollection = ViewModel.InteractiveCollectionFactory.Build(
                    GetKeyFunc(Key),
                    ItemsSourceSubject.Select(v => v.Cast <object>()),
                    FilterSubject,
                    DeletedSubject.WithLatestFrom(RemoveSubject.StartWith(Remove).DistinctUntilChanged(), (d, r) => r ? d : null).Where(v => v != null),
                    ClearedSubject,
                    UI, null, IsReadOnly
                    );
                CollectionChanged();
            }

            Init();
        }
Exemplo n.º 5
0
        public virtual ViewModel.InteractiveCollectionViewModel <object, IConvertible> React(/*string key,*/ string childrenpath, IEnumerable enumerable, IObservable <bool> ischecked, System.Reactive.Concurrency.DispatcherScheduler UI, System.Windows.Threading.Dispatcher dispatcher)
        {
            var sx = ObservableChangeSet.Create <object, IConvertible>(cache =>
            {
                foreach (var val in enumerable)
                {
                    cache.AddOrUpdate(val);
                }
                return(System.Reactive.Disposables.Disposable.Empty);
            }, GetKey);

            var kx = new ViewModel.InteractiveCollectionViewModel <object, IConvertible>(sx, ChildrenPath, ischecked, ExpandSubject.StartWith(Expand).DistinctUntilChanged(), UI, dispatcher);

            kx.GetChecked();
            kx.GetSelectedItem(ischecked).Subscribe(_ =>
            {
                this.Dispatcher.InvokeAsync(() => SelectedItem = _, System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            });
            kx.GetCheckedChildItems(ischecked, childrenpath).Subscribe(_ =>
            {
                this.Dispatcher.InvokeAsync(() => CheckedItems = _, System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            });

            AllCheckedItems = kx.@checked;

            //kx.GetSelected().WithLatestFrom(ischecked,(a,b)=>new { a, b }).Subscribe(_=>
            //{
            //    if (@checked.Contains(_) || _.b==false)
            //    {
            //        this.Dispatcher.InvokeAsync(() => SelectedItem = _.a, System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            //        this.Dispatcher.InvokeAsync(() => CheckedItems = ReflectionHelper.RecursivePropValues(_.a, childrenpath).Cast<object>().Where(a => @checked.Contains(a)).ToList(), System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            //    }
            //});

            //kx.ChildSubject.Where(_=>_.Value.Interaction==Interaction.Select &&((int) _.Value.Value)>0).WithLatestFrom(ischecked, (a, b) => new { a, b }).Subscribe(_ =>
            //{
            //    if (@checked.Contains(_.a.Key) || _.b == false)
            //    {
            //        this.Dispatcher.InvokeAsync(() => SelectedItem = _.a.Key, System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            //        this.Dispatcher.InvokeAsync(() => CheckedItems = ReflectionHelper.RecursivePropValues(_.a.Key, childrenpath).Cast<object>().Where(a => @checked.Contains(a)).ToList(), System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            //    }
            //});

            //kx.ChildSubject.Where(_ => _.Value.Interaction == Interaction.Check).Subscribe(_ =>
            //{
            //    if (!((bool)_.Value.Value))
            //        if (@checked.Contains(_.Key))
            //        {
            //            @checked.Remove(_.Key);
            //            this.Dispatcher.InvokeAsync(() => SelectedItem = null, System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            //            this.Dispatcher.InvokeAsync(() => CheckedItems = null, System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            //        }

            //   else if (((bool)_.Value.Value))
            //            if (@unchecked.Contains(_.Key))
            //            {
            //                @unchecked.Remove(_.Key);
            //                this.Dispatcher.InvokeAsync(() => SelectedItem = _.Key, System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            //                this.Dispatcher.InvokeAsync(() => CheckedItems = ReflectionHelper.RecursivePropValues(_.Key, childrenpath).Cast<object>().Where(a => @checked.Contains(a)).ToList(), System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            //            }

            //});

            //kx.DoubleClicked.Subscribe(_ =>
            //{
            //    this.Dispatcher.InvokeAsync(() => DoubleClickedItem = _, System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            //});

            //SelectedItemSubject.Subscribe(_ =>

            //kx.Deleted.Subscribe(_ =>
            //{
            //    this.Dispatcher.InvokeAsync(() => Deleted = _, System.Windows.Threading.DispatcherPriority.Background, default(System.Threading.CancellationToken));
            //});
            return(kx);
        }