public GenericSelectorDialogViewModel(IEnumerable <ColumnDescriptor> columns,
                                              IEnumerable <T> collection,
                                              Func <T, int> entryGetter,
                                              Func <T, string> index)
        {
            this.entryGetter = entryGetter;
            this.index       = index;
            items            = new SourceList <T>();
            ReadOnlyObservableCollection <T> l;
            var currentFilter = this.WhenValueChanged(t => t.SearchText).Select <string?, Func <T, bool> >(val =>
            {
                if (string.IsNullOrEmpty(val))
                {
                    return(model => true);
                }
                var lowerCase = val.ToLower();
                return(model => index(model).ToLower().Contains(lowerCase));
            });

            items
            .Connect()
            .Filter(currentFilter, ListFilterPolicy.ClearAndReplace)
            .Sort(Comparer <T> .Create((x, y) => entryGetter(x).CompareTo(entryGetter(y))))
            .Bind(out l)
            .Subscribe();
            FilteredItems = l;

            Columns = new ObservableCollection <ColumnDescriptor>();

            foreach (var column in columns)
            {
                Columns.Add(column);
            }

            items.AddRange(collection);

            Accept = new DelegateCommand(() =>
            {
                if (SelectedItem == null && FilteredItems.Count == 1)
                {
                    SelectedItem = FilteredItems[0];
                }
                CloseOk?.Invoke();
            }, () => SelectedItem != null || FilteredItems.Count == 1 || int.TryParse(SearchText, out _));
            Cancel = new DelegateCommand(() => CloseCancel?.Invoke());

            FilteredItems.ObserveCollectionChanges().Subscribe(_ => Accept.RaiseCanExecuteChanged());
            this.WhenPropertyChanged(t => t.SearchText).Subscribe(_ => Accept.RaiseCanExecuteChanged());
            this.WhenPropertyChanged(t => t.SelectedItem).Subscribe(_ => Accept.RaiseCanExecuteChanged());
        }
예제 #2
0
        public ItemFromListProviderViewModel(Dictionary <T, SelectOption>?items,
                                             IComparer <CheckableSelectOption <T> > comparer,
                                             Func <T, bool> shouldBeSelected,
                                             bool asFlags,
                                             T?current = default)
        {
            Items        = items;
            this.asFlags = asFlags;

            this.items = AutoDispose(new SourceList <CheckableSelectOption <T> >());
            ReadOnlyObservableCollection <CheckableSelectOption <T> > outFilteredList;

            currentFilter = AutoDispose(new ReactiveProperty <Func <CheckableSelectOption <T>, bool> >(_ => true,
                                                                                                       Compare.Create <Func <CheckableSelectOption <T>, bool> >((_, _) => false, _ => 0)));
            AutoDispose(this.items.Connect()
                        .Filter(currentFilter)
                        .Sort(comparer)
                        .Bind(out outFilteredList)
                        .Subscribe());
            FilteredItems = outFilteredList;

            if (items != null)
            {
                this.items.Edit(list =>
                {
                    foreach (T key in items.Keys)
                    {
                        bool isSelected = shouldBeSelected(key);
                        var item        = new CheckableSelectOption <T>(key, items[key], isSelected);
                        if (isSelected)
                        {
                            SelectedItem = item;
                        }
                        list.Add(item);
                    }
                });
            }

            Columns = new ObservableCollection <ColumnDescriptor>
            {
                new("Key", "Entry", 80),
                new("Name", "Name", 220),
                new("Description", "Description", 320)
            };

            if (asFlags)
            {
                Columns.Insert(0, new ColumnDescriptor("", "IsChecked", 35, true));
            }

            if (items == null || items.Count == 0)
            {
                SearchText = current != null?current.ToString() ?? "" : "";
            }

            ShowItemsList = items?.Count > 0;
            DesiredHeight = ShowItemsList ? 670 : 130;
            DesiredWidth  = ShowItemsList ? 800 : 400;
            Accept        = new DelegateCommand(() =>
            {
                if (SelectedItem == null && FilteredItems.Count == 1)
                {
                    SelectedItem = FilteredItems[0];
                }
                CloseOk?.Invoke();
            }, () => asFlags || SelectedItem != null || FilteredItems.Count == 1 || int.TryParse(SearchText, out _));
            Cancel = new DelegateCommand(() => CloseCancel?.Invoke());

            FilteredItems.ObserveCollectionChanges().Subscribe(_ => Accept.RaiseCanExecuteChanged());
            this.WhenPropertyChanged(t => t.SearchText).Subscribe(_ => Accept.RaiseCanExecuteChanged());
            this.WhenPropertyChanged(t => t.SelectedItem).Subscribe(_ => Accept.RaiseCanExecuteChanged());
        }
        public ItemFromListProviderViewModel(Dictionary <long, SelectOption> items, bool asFlags, long?current = null)
        {
            this.asFlags = asFlags;

            this.items = AutoDispose(new SourceList <CheckableSelectOption>());
            ReadOnlyObservableCollection <CheckableSelectOption> outFilteredList;

            currentFilter = AutoDispose(new ReactiveProperty <Func <CheckableSelectOption, bool> >(_ => true,
                                                                                                   Compare.Create <Func <CheckableSelectOption, bool> >((_, _) => false, _ => 0)));
            AutoDispose(this.items.Connect()
                        .Filter(currentFilter)
                        .Sort(Comparer <CheckableSelectOption> .Create((x, y) => x.Entry.CompareTo(y.Entry)))
                        .Bind(out outFilteredList)
                        .Subscribe());
            FilteredItems = outFilteredList;

            this.items.Edit(list =>
            {
                foreach (long key in items.Keys)
                {
                    bool isSelected = current.HasValue && ((current == 0 && key == 0) || (key > 0) && (current & key) == key);
                    var item        = new CheckableSelectOption(key, items[key], isSelected);
                    if (isSelected)
                    {
                        SelectedItem = item;
                    }
                    list.Add(item);
                }
            });

            Columns = new ObservableCollection <ColumnDescriptor>
            {
                new("Key", "Entry", 50),
                new("Name", "Name"),
                new("Description", "Description")
            };

            if (asFlags)
            {
                Columns.Insert(0, new ColumnDescriptor("", "IsChecked", 35, true));
            }

            if (items.Count == 0)
            {
                SearchText = current.HasValue ? current.Value.ToString() : "";
            }

            ShowItemsList = items.Count > 0;
            DesiredHeight = ShowItemsList ? 470 : 130;
            Accept        = new DelegateCommand(() =>
            {
                if (SelectedItem == null && FilteredItems.Count == 1)
                {
                    SelectedItem = FilteredItems[0];
                }
                CloseOk?.Invoke();
            }, () => asFlags || SelectedItem != null || FilteredItems.Count == 1 || int.TryParse(SearchText, out _));
            Cancel = new DelegateCommand(() => CloseCancel?.Invoke());

            FilteredItems.ObserveCollectionChanges().Subscribe(_ => Accept.RaiseCanExecuteChanged());
            this.WhenPropertyChanged(t => t.SearchText).Subscribe(_ => Accept.RaiseCanExecuteChanged());
            this.WhenPropertyChanged(t => t.SelectedItem).Subscribe(_ => Accept.RaiseCanExecuteChanged());
        }