예제 #1
0
        public static IDisposable Binding <TFilter, TObject>(
            this AutoCompleteBox acb,
            IFilterViewModel <TFilter, TObject> filterViewModel,
            Action <IFilterViewModel <TFilter, TObject>, string> action = null,
            Action <bool> busy = null,
            int startWith      = 1)
        {
            var disposableObject = new DisposableObject();

            var searchTextEntered = acb.WhenAny(x => x.SearchText, x => x.Value)
                                    .Do(_ => filterViewModel.Abort())
                                    .Throttle(TimeSpan.FromSeconds(1))
                                    .Where(text => !string.IsNullOrWhiteSpace(text))
                                    .Where(text => text.Length >= startWith)
                                    .ObserveOnUiSafeScheduler()
                                    .Do(text =>
            {
                if (action != null)
                {
                    action(filterViewModel, string.Concat(text, "*"));
                }
            });

            disposableObject.Add(
                searchTextEntered.Subscribe(
                    _ =>
            {
                if (filterViewModel.Result != null)
                {
                    filterViewModel.Result.Clear();
                }

                filterViewModel.InvokeCommand.Execute(null);
            }));

            var loadCompleted = filterViewModel.WhenAny(x => x.Result, x => x.Value)
                                .Where(x => x != null && x.Count > 0);

            disposableObject.Add(loadCompleted.ObserveOnUiSafeScheduler().Subscribe(
                                     result =>
            {
                acb.ItemsSource = result;
                if (acb.IsKeyboardFocusWithin)
                {
                    acb.IsDropDownOpen = true;
                }
            }));

            if (busy != null)
            {
                disposableObject.Add(filterViewModel.WhenAnyValue(x => x.IsBusy).Subscribe(busy));
            }

            return(disposableObject);
        }