Ejemplo n.º 1
0
        public ListBoxPageViewModel()
        {
            Items     = new ObservableCollection <string>(Enumerable.Range(1, 10000).Select(i => GenerateItem()));
            Selection = new SelectionModel <string>();
            Selection.Select(1);

            AddItemCommand = ReactiveCommand.Create(() => Items.Add(GenerateItem()));

            RemoveItemCommand = ReactiveCommand.Create(() =>
            {
                while (Selection.Count > 0)
                {
                    Items.Remove(Selection.SelectedItems.First());
                }
            });

            SelectRandomItemCommand = ReactiveCommand.Create(() =>
            {
                var random = new Random();

                using (Selection.BatchUpdate())
                {
                    Selection.Clear();
                    Selection.Select(random.Next(Items.Count - 1));
                }
            });
        }
        public ListBoxPageViewModel()
        {
            Items = new ObservableCollection <string>(Enumerable.Range(1, 10000).Select(i => GenerateItem()));

            Selection = new SelectionModel <string>();
            Selection.Select(1);

            _selectionMode = this.WhenAnyValue(
                x => x.Multiple,
                x => x.Toggle,
                x => x.AlwaysSelected,
                (m, t, a) =>
                (m ? SelectionMode.Multiple : 0) |
                (t ? SelectionMode.Toggle : 0) |
                (a ? SelectionMode.AlwaysSelected : 0))
                             .ToProperty(this, x => x.SelectionMode);

            AddItemCommand = ReactiveCommand.Create(() => Items.Add(GenerateItem()));

            RemoveItemCommand = ReactiveCommand.Create(() =>
            {
                var items = Selection.SelectedItems.ToList();

                foreach (var item in items)
                {
                    Items.Remove(item);
                }
            });

            SelectRandomItemCommand = ReactiveCommand.Create(() =>
            {
                var random = new Random();

                using (Selection.BatchUpdate())
                {
                    Selection.Clear();
                    Selection.Select(random.Next(Items.Count - 1));
                }
            });
        }