Exemple #1
0
        public DependencyObject ContainerFromIndex(int index)
        {
            DependencyObject container;

            ContainerIndexMap.TryMap(index, out container);
            return(container);
        }
Exemple #2
0
 internal void RemoveAll()
 {
     foreach (var pair in ContainerItemMap)
     {
         Owner.ClearContainerForItem(pair.Key, pair.Value);
     }
     RealizedElements.Clear();
     ContainerIndexMap.Clear();
     ContainerItemMap.Clear();
 }
Exemple #3
0
        public int IndexFromContainer(DependencyObject container)
        {
            int index;

            if (!ContainerIndexMap.TryMap(container, out index))
            {
                index = -1;
            }
            return(index);
        }
Exemple #4
0
        internal void Remove(GeneratorPosition position, int count)
        {
            CheckOffsetAndRealized(position, count);

            int index = IndexFromGeneratorPosition(position);

            for (int i = 0; i < count; i++)
            {
                var container = ContainerIndexMap [index + i];
                var item      = ContainerItemMap [container];
                ContainerIndexMap.Remove(container, index + i);
                ContainerItemMap.Remove(container);
                RealizedElements.Remove(index + i);
                Owner.ClearContainerForItem(container, item);
            }
        }
Exemple #5
0
        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;
        }
Exemple #6
0
        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);
        }