This is an old revision of the document!
List and ObservableCollections
Create a custom type:
public enum TColorType { Red, Green, Blue, } public class TColor { public string Name { get; set; } public TColorType Type { get; set; } }
Create a color list:
public class TColorList: List<TColor> { ///---------------------------------------------------------------------------------------- /// Constructor ///---------------------------------------------------------------------------------------- public TColorList() { PopulateList(); } private void PopulateList() { Add(new TColor() { Name = "Red", Type = TColorType.Red }); Add(new TColor() { Name = "Green", Type = TColorType.Green }); Add(new TColor() { Name = "Blue", Type = TColorType.Blue }); } }
Create a color collection:
public class TColorCollection: ObservableCollection<TColor> { ///---------------------------------------------------------------------------------------- /// <summary> /// Indexer overload. /// </summary> /// <param name="aColorString">Color (string)</param> /// <returns>TColor</returns> ///---------------------------------------------------------------------------------------- public TColor this[string aColorString] { get { return this.Where(n => n.Type.ToString().ToLower() == aColorString.ToLower()).SingleOrDefault(); } } ///---------------------------------------------------------------------------------------- /// <summary> /// Indexer overload. /// </summary> /// <param name="aColor">>Color</param> /// <returns>TColor</returns> ///---------------------------------------------------------------------------------------- public TColor this[TColorType aColorType] { get { return this.Where(n => n.Type == aColorType).SingleOrDefault(); } } ///---------------------------------------------------------------------------------------- /// Constructor ///---------------------------------------------------------------------------------------- public TColorCollection() { PopulateCollection(); } private void PopulateCollection() { Add(new TColor() { Name = "Red", Type = TColorType.Red }); Add(new TColor() { Name = "Green", Type = TColorType.Green }); Add(new TColor() { Name = "Blue", Type = TColorType.Blue }); } }