/// <summary>
 /// Creates and adds DataItems for the specified SeriesDefinition's items.
 /// </summary>
 /// <param name="definition">Specified SeriesDefinition.</param>
 /// <param name="items">Sequence of items.</param>
 /// <param name="startingIndex">Starting index.</param>
 private void AddDataItems(SeriesDefinition definition, IEnumerable<object> items, int startingIndex)
 {
     int index = startingIndex;
     foreach (object item in items)
     {
         DataItems.Add(new DataItem(definition) { Value = item, Index = index });
         index++;
     }
     // Because properties (like DependentValueBinding) may still be getting set
     //Dispatcher.BeginInvoke((Action)AddedDataItems);
     AddedDataItems();
 }
        internal void SeriesDefinitionItemsSourceCollectionChanged(SeriesDefinition definition, NotifyCollectionChangedAction action, IList oldItems, int oldStartingIndex, IList newItems, int newStartingIndex)
        {
            if (NotifyCollectionChangedAction.Replace == action)
            {
                // Perform in-place replacements
                foreach (DataItem dataItem in DataItems.Where(di => (di.SeriesDefinition == definition) && (newStartingIndex <= di.Index) && (di.Index < newStartingIndex + newItems.Count)))
                {
                    dataItem.Value = newItems[dataItem.Index - newStartingIndex];
                }
            }
            else
            {
                if (NotifyCollectionChangedAction.Reset == action)
                {
                    // Set up parameters to allow normal old/new item handling to be used
                    Debug.Assert(null == oldItems, "Reset action with non-null oldItems.");
                    oldItems = DataItems.Where(di => (di.SeriesDefinition == definition)).ToArray();
                    oldStartingIndex = 0;
                    newItems = definition.ItemsSource.CastWrapper<object>().ToArray();
                    newStartingIndex = 0;
                }
                if (null != oldItems)
                {
                    // Get rid of old items
                    foreach (DataItem oldDataItem in DataItems.Where(di => (di.SeriesDefinition == definition) && (oldStartingIndex <= di.Index) && (di.Index < oldStartingIndex + oldItems.Count)))
                    {
                        oldDataItem.Index = -1;
                        if (null != oldDataItem.DataPoint)
                        {
                            oldDataItem.DataPoint.State = DataPointState.Hiding;
                        }
                    }
                    // Adjust index of shifted items
                    foreach (DataItem dataItem in DataItems.Where(di => (di.SeriesDefinition == definition) && (oldStartingIndex + oldItems.Count <= di.Index)))
                    {
                        dataItem.Index -= oldItems.Count;
                    }
                }
                if (null != newItems)
                {
                    // Adjust index of shifted items
                    foreach (DataItem dataItem in DataItems.Where(di => (di.SeriesDefinition == definition) && (newStartingIndex <= di.Index)))
                    {
                        dataItem.Index += newItems.Count;
                    }
                    // Add new items
                    AddDataItems(definition, newItems.CastWrapper<object>(), newStartingIndex);
                }
            }
#if DEBUG
            // Validate all DataItem index and value properties
            foreach (var group in DataItems.Where(di => 0 <= di.Index).OrderBy(di => di.Index).GroupBy(di => di.SeriesDefinition))
            {
                object[] items = group.Key.ItemsSource.CastWrapper<object>().ToArray();
                int i = 0;
                foreach (DataItem dataItem in group)
                {
                    Debug.Assert(i == dataItem.Index, "DataItem index mis-match.");
                    Debug.Assert(dataItem.Value.Equals(items[i]), "DataItem value mis-match.");
                    i++;
                }
            }
#endif
        }
 /// <summary>
 /// Updates the palette properties of the specified SeriesDefinition.
 /// </summary>
 /// <param name="definition">Specified SeriesDefinition.</param>
 private void UpdatePaletteProperties(SeriesDefinition definition)
 {
     ResourceDictionary resources = null;
     if (null != SeriesHost)
     {
         Type dataPointType = CreateDataPoint().GetType();
         using (IEnumerator<ResourceDictionary> enumerator = SeriesHost.GetResourceDictionariesWhere(dictionary =>
         {
             Style style = dictionary["DataPointStyle"] as Style;
             if (null != style)
             {
                 return true;//(null != style.TargetType) && (style.TargetType.IsAssignableFrom(dataPointType));
             }
             return false;
         }))
         {
             if (enumerator.MoveNext())
             {
                 resources = enumerator.Current;
             }
         }
     }
     definition.PaletteDataPointStyle = (null != resources) ? resources["DataPointStyle"] as Style : null;
     definition.PaletteDataShapeStyle = (null != resources) ? resources["DataShapeStyle"] as Style : null;
     //definition.PaletteLegendItemStyle = (null != resources) ? resources["LegendItemStyle"] as Style : null;
 }
 /// <summary>
 /// Handles changes to the ItemsSource of a SeriesDefinition.
 /// </summary>
 /// <param name="definition">SeriesDefinition owner.</param>
 /// <param name="oldValue">Old value.</param>
 /// <param name="newValue">New value.</param>
 internal void SeriesDefinitionItemsSourceChanged(SeriesDefinition definition, IEnumerable oldValue, IEnumerable newValue)
 {
     if (null != oldValue)
     {
         foreach (DataItem dataItem in DataItems.Where(di => di.SeriesDefinition == definition).ToArray())
         {
             DataItems.Remove(dataItem);
         }
         RemovedDataItems();
     }
     if (null != newValue)
     {
         // No need to add items if SeriesHost null; setting SeriesHost will take care of that
         if (null != SeriesHost)
         {
             AddDataItems(definition, newValue.CastWrapper<object>(), 0);
         }
     }
 }
 /// <summary>
 /// Initializes a new instance of the DataItem class.
 /// </summary>
 /// <param name="seriesDefinition">SeriesDefinition owner.</param>
 public DataItem(SeriesDefinition seriesDefinition)
 {
     SeriesDefinition = seriesDefinition;
     CenterPoint = new Point(double.NaN, double.NaN);
 }