using System; using System.Collections; using System.Collections.Generic; using System.Windows; namespace Microsoft.Research.DynamicDataDisplay.DataSources { public class EnumerableDataSource : EnumerableDataSourceBase { public EnumerableDataSource(IEnumerable data) : base(data) { } public EnumerableDataSource(IEnumerable data) : base(data) { } private readonly List> mappings = new List>(); private Func xMapping; private Func yMapping; private Func xyMapping; public Func XMapping { get { return xMapping; } set { SetXMapping(value); } } public Func YMapping { get { return yMapping; } set { SetYMapping(value); } } public Func XYMapping { get { return xyMapping; } set { SetXYMapping(value); } } public void SetXMapping(Func mapping) { if (mapping == null) throw new ArgumentNullException("mapping"); this.xMapping = mapping; RaiseDataChanged(); } public void SetYMapping(Func mapping) { if (mapping == null) throw new ArgumentNullException("mapping"); this.yMapping = mapping; RaiseDataChanged(); } public void SetXYMapping(Func mapping) { if (mapping == null) throw new ArgumentNullException("mapping"); this.xyMapping = mapping; RaiseDataChanged(); } public void AddMapping(DependencyProperty property, Func mapping) { if (property == null) throw new ArgumentNullException("property"); if (mapping == null) throw new ArgumentNullException("mapping"); mappings.Add(new Mapping { Property = property, F = mapping }); } public override IPointEnumerator GetEnumerator(DependencyObject context) { return new EnumerablePointEnumerator(this); } internal void FillPoint(T elem, ref Point point) { if (xyMapping != null) { point = xyMapping(elem); } else { if (xMapping != null) { point.X = xMapping(elem); } if (yMapping != null) { point.Y = yMapping(elem); } } } internal void ApplyMappings(DependencyObject target, T elem) { if (target != null) { foreach (var mapping in mappings) { target.SetValue(mapping.Property, mapping.F(elem)); } } } } }