ReactiveUI. Excluding already chosen items.

This situation is quite common. Only certain items are available for selection. In the sample, we have 3 hotel rooms and 3 guests. When you assign one guest to a room and want to assign another guest, the first shouldn’t appear on the list of selectable guests. This is done using a derived collection with a filter.

person => !person.IsAdded || (person.IsAdded && Equals(person, SelectedPerson))

Untitled

using System.Collections.Generic;
using System.Linq;
using MEFedMVVM.ViewModelLocator;
using ReactiveUI;

namespace WpfApplication3
{
    [ExportViewModel("Main")]
    public class MainViewModel : ReactiveObject
    {
        public MainViewModel()
        {
            var people = new ReactiveList<Person>
            {
                new Person("Jim"),
                new Person("Tom"),
                new Person("Jack"),
            };
            people.ChangeTrackingEnabled = true;

            Rooms = new ReactiveList<Room>(people.Select((person, i) => new Room(people, string.Format("Room {0}", i))));
        }

        public IReactiveList<Room> Rooms { get; private set; }
    }

    public class Room : ReactiveObject
    {
        private readonly string name;
        private Person selectedPerson;

        public Room(IEnumerable<Person> people, string name)
        {
            this.name = name;
            People = people.CreateDerivedCollection(person => person, person => !person.IsAdded || (person.IsAdded && Equals(person, SelectedPerson)));
        }

        public IReactiveDerivedList<Person> People { get; set; }

        public Person SelectedPerson
        {
            get { return selectedPerson; }
            set
            {
                if (selectedPerson != null)
                {
                    selectedPerson.IsAdded = false;
                }

                if (value != null)
                {
                    value.IsAdded = true;
                }

                this.RaiseAndSetIfChanged(ref selectedPerson, value);
            }
        }

        public string Name
        {
            get { return name; }
        }
    }

    public class Person : ReactiveObject
    {
        private readonly string name;
        private bool isAdded;

        public Person(string name)
        {
            this.name = name;
        }

        public string Name
        {
            get { return name; }
        }

        public bool IsAdded
        {
            get { return isAdded; }
            set { this.RaiseAndSetIfChanged(ref isAdded, value); }
        }

        private bool Equals(Person other)
        {
            return string.Equals(name, other.name);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((Person)obj);
        }

        public override int GetHashCode()
        {
            return name.GetHashCode();
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s