// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Collections { using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Linq; /// /// Implements the interface for single items. /// /// The type of the single item. /// /// Classes such as Border can only ever have a single logical child, but they need to /// implement a list of logical children in their ILogical.LogicalChildren property using the /// interface. This class facilitates that /// without creating an actual . /// public class PerspexSingleItemList : IPerspexReadOnlyList where T : class { private T item; public PerspexSingleItemList() { } public PerspexSingleItemList(T item) { this.item = item; } public event NotifyCollectionChangedEventHandler CollectionChanged; public event PropertyChangedEventHandler PropertyChanged; public int Count { get { return this.item != null ? 1 : 0; } } public T SingleItem { get { return this.item; } set { NotifyCollectionChangedEventArgs e = null; bool countChanged = false; if (value == null && this.item != null) { e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, this.item, 0); this.item = null; countChanged = true; } else if (value != null && this.item == null) { e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, this.item, 0); this.item = value; countChanged = true; } else { e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, value, this.item, 0); this.item = value; } if (e != null && this.CollectionChanged != null) { this.CollectionChanged(this, e); } if (countChanged && this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs("Count")); } } } public T this[int index] { get { if (index < 0 || index >= this.Count) { throw new ArgumentOutOfRangeException(); } return this.item; } } public IEnumerator GetEnumerator() { return Enumerable.Repeat(this.item, this.Count).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } } }