void MoveExistingItems(int index, int offset) { // This is a little horrible. I should really collapse the existing // RangeCollection so that every > the current index is decremented by 1. // This is easier for now though. I may think of a better way later on. RangeCollection newRanges = new RangeCollection(); List <int> list = new List <int> (); for (int i = 0; i < RealizedElements.Count; i++) { list.Add(RealizedElements [i]); } if (offset > 0) { list.Reverse(); } foreach (int i in list) { int oldIndex = i; if (oldIndex < index) { newRanges.Add(oldIndex); } else { newRanges.Add(oldIndex + offset); var container = ContainerIndexMap [oldIndex]; ContainerIndexMap.Remove(container, oldIndex); ContainerIndexMap.Add(container, oldIndex + offset); } } RealizedElements = newRanges; }
internal DependencyObject GenerateNext(out bool isNewlyRealized) { if (GenerationState == null) { throw new InvalidOperationException("Cannot call GenerateNext before calling StartAt"); } int index; // This is relative to the realised elements. int startAt = GenerationState.Position.Index; if (startAt == -1) { if (GenerationState.Position.Offset < 0) { index = Owner.Items.Count + GenerationState.Position.Offset; } else if (GenerationState.Position.Offset == 0) { index = 0; } else { index = GenerationState.Position.Offset - 1; } } else if (startAt >= 0 && startAt < RealizedElements.Count) { // We're starting relative to an already realised element index = RealizedElements [startAt] + GenerationState.Position.Offset; } else { index = -1; } bool alreadyRealized = RealizedElements.Contains(index); if (!GenerationState.AllowStartAtRealizedItem && alreadyRealized && GenerationState.Position.Offset == 0) { index += GenerationState.Step; alreadyRealized = RealizedElements.Contains(index); } if (index < 0 || index >= Owner.Items.Count) { isNewlyRealized = false; return(null); } if (alreadyRealized) { GenerationState.Position = new GeneratorPosition(RealizedElements.IndexOf(index), GenerationState.Step); isNewlyRealized = false; return(ContainerIndexMap [index]); } DependencyObject container; var item = Owner.Items [index]; if (Owner.IsItemItsOwnContainer(item)) { container = (DependencyObject)item; isNewlyRealized = true; } else { if (Cache.Count == 0) { container = Owner.GetContainerForItem(); isNewlyRealized = true; } else { container = Cache.Dequeue(); isNewlyRealized = false; } ContentControl c = container as ContentControl; if (c != null) { c.ContentSetsParent = false; } } FrameworkElement f = container as FrameworkElement; if (f != null && !(item is UIElement)) { f.DataContext = item; } RealizedElements.Add(index); ContainerIndexMap.Add(container, index); ContainerItemMap.Add(container, item); GenerationState.Position = new GeneratorPosition(RealizedElements.IndexOf(index), GenerationState.Step); return(container); }