Click here to Skip to main content
15,895,011 members
Articles / General Programming / Performance

Implement Selectable Virtual List

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
3 Jan 2013CPOL8 min read 30.1K   540   15  
This article is Part 2 of the data display performance optimizing series. The Selectable Virtual List is a list where you can select individual items in the list, and move it out or in to the list. You can also use the select all checkbox to select all items in the list.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace SelectableVirtualList
{
	public class ViewModel : INotifyPropertyChanged
	{
		public ObservableCollection<Employee> SelectedEmployeeCollection
		{
			get; 
			set;
		}

		private SelectableVirtualList<Employee> _AvailableEmployeeCollection;
		public SelectableVirtualList<Employee> AvailableEmployeeCollection
		{
			get { return _AvailableEmployeeCollection; }
			set
			{
				_AvailableEmployeeCollection = value;
				OnPropertyChanged("AvailableEmployeeCollection");
			}
		}

		public ViewModel()
		{
			AvailableEmployeeCollection = new SelectableVirtualList<Employee>(new EmployeeObjectGenerator());
			SelectedEmployeeCollection = new ObservableCollection<Employee>();
		}

		public void Add()
		{
			IList<Employee> employees = AvailableEmployeeCollection.SelectedList;
			foreach (var employee in employees)
			{
				employee.IsSelected = false;
				if (!SelectedEmployeeCollection.Contains(employee))
				{
					SelectedEmployeeCollection.Add(employee);
				}
				AvailableEmployeeCollection.Remove(employee);
			}
		}

		public void Remove()
		{
			int internalStandardCount = SelectedEmployeeCollection.Count;
			for (int i = internalStandardCount - 1; i >= 0; i--)
			{
				Employee answerInstance = SelectedEmployeeCollection[i];
				if (answerInstance.IsSelected)
				{
					answerInstance.IsSelected = false;
					if (!AvailableEmployeeCollection.Contains(answerInstance))
					{
						AvailableEmployeeCollection.Add(answerInstance);
					}
					SelectedEmployeeCollection.Remove(answerInstance);
				}
			}
		}

		public void SelectAvailableList()
		{
			AvailableEmployeeCollection.SelectAll();
			OnPropertyChanged("AvailableEmployeeCollection");
		}

		public void DeselectAvailableList()
		{
			AvailableEmployeeCollection.DeSelectAll();
			OnPropertyChanged("AvailableEmployeeCollection");
		}

		public event PropertyChangedEventHandler PropertyChanged;
		public void OnPropertyChanged(string name)
		{
			PropertyChangedEventHandler handler = PropertyChanged;
			if (handler != null)
			{
				handler(this, new PropertyChangedEventArgs(name));
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Canada Canada
I am a passionated software developer / engineer with strong desire to develop in a simple, fast, beautiful way with the skillset such as simple design, refactoring, TDD. i worked in J2EE for about 5 years, now i work as a .NET devleoper.

Comments and Discussions