コード例 #1
0
        public void AddInFilledCache()
        {
            var target = new SizesCache();
            target.AddOrChange(0, 43);
            target.AddOrChange(0, 46);
            target.AddOrChange(0, 46);

            Assert.AreEqual(46, target.GetEstimate(0));
        }
コード例 #2
0
        public void CleanUpFilledCacheNormalCase()
        {
            var target = new SizesCache();
            target.AddOrChange(0, 40);
            target.AddOrChange(0, 41);
            target.AddOrChange(0, 42);
            target.AddOrChange(0, 43);

            target.CleanUp(0);
            Assert.AreEqual(0, target.GetEstimate(0));
        }
コード例 #3
0
        public void AddInEmptyCache()
        {
            var expected = 43.0;
            var target = new SizesCache();
            target.AddOrChange(0, expected);

            Assert.AreEqual(expected, target.GetEstimate(0));
        }
コード例 #4
0
 private void RegisterHeight(TreeViewEx tree, int level, double size)
 {
     cachedSizes.AddOrChange(0, size);
     tree.cachedSizes.AddOrChange(level, size);
 }
コード例 #5
0
        /// <summary>
        /// Measure the children
        /// </summary>
        /// <param name="availableSize">Size available</param>
        /// <returns>Size desired</returns>
        protected override Size MeasureOverride(Size availableSize)
        {
            if (ScrollOwner != null)
            {
                if (ScrollOwner.ScrollableWidth < HorizontalOffset)
                {
                    SetHorizontalOffset(ScrollOwner.ScrollableWidth);
                }
                if (ScrollOwner.ScrollableHeight < VerticalOffset)
                {
                    SetVerticalOffset(ScrollOwner.ScrollableHeight);
                }
            }

            // We need to access InternalChildren before the generator to work around a bug
            UIElementCollection     children     = InternalChildren;
            IItemContainerGenerator generator    = ItemContainerGenerator;
            ItemsControl            itemsControl = ItemsControl.GetItemsOwner(this);
            TreeViewExItem          treeViewItem = itemsControl as TreeViewExItem;
            TreeViewEx treeView = itemsControl as TreeViewEx ?? treeViewItem.ParentTreeView;

            double currentY = 0;
            double maxWidth = 0;
            int    firstVisibleItemIndex = 0;
            int    lastVisibleItemIndex  = 0;

            if (itemsControl.HasItems)
            {
                if (treeView.IsVirtualizing)
                {
                    double bottomY = VerticalOffset + availableSize.Height;

                    //add sizes of not visible items before visible ones to currentX
                    for (int i = 0; i < itemsControl.Items.Count; i++)
                    {
                        // get height, maybe it is estimated
                        double height = GetContainerHeightForItem(itemsControl, i);

                        if (currentY + height >= VerticalOffset && currentY <= bottomY)
                        {
                            firstVisibleItemIndex = i;
                            lastVisibleItemIndex  = i;

                            // we found the first visible item, lets realize it and all other visible items. while we
                            // do so, we take care of counting i up
                            GeneratorPosition startPos = generator.GeneratorPositionFromIndex(firstVisibleItemIndex);
                            int itemGeneratorIndex     = (startPos.Offset == 0) ? startPos.Index : startPos.Index + 1;

                            using (generator.StartAt(startPos, GeneratorDirection.Forward, true))
                            {
                                // create items until current y is bigger than bottomy => we reached the last visible item
                                while (currentY <= bottomY && i < itemsControl.Items.Count)
                                {
                                    // Get or create the child
                                    bool           newlyRealized;
                                    TreeViewExItem child = generator.GenerateNext(out newlyRealized) as TreeViewExItem;
                                    if (newlyRealized)
                                    {
                                        // Figure out if we need to insert the child at the end or somewhere in the middle
                                        AddOrInsertItemToInternalChildren(itemGeneratorIndex, child);
                                        child.ParentTreeView = treeView;
                                        generator.PrepareItemContainer(child);
                                    }
                                    else
                                    {
                                        // The child has already been created, let's be sure it's in the right spot
                                        if (child != children[itemGeneratorIndex])
                                        {
                                            throw new InvalidOperationException("Wrong child was generated");
                                        }
                                    }

                                    child.Measure(new Size(double.MaxValue, double.MaxValue));

                                    // now get the real height
                                    height = child.DesiredSize.Height;
                                    // add real height to cache
                                    cachedSizes.AddOrChange(i, height);
                                    // add real height to current position
                                    currentY += height;
                                    // save the maximum needed width
                                    maxWidth = Math.Max(maxWidth, child.DesiredSize.Width);

                                    lastVisibleItemIndex++;
                                    i++;
                                    itemGeneratorIndex++;

                                    //break realization if we reach the bottom of control
                                    if (currentY > bottomY)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            currentY += height;
                        }
                    }

                    // Note: this could be deferred to idle time for efficiency
                    CleanUpItems(firstVisibleItemIndex, lastVisibleItemIndex);
                }
                else
                {
                    GeneratorPosition startPos = generator.GeneratorPositionFromIndex(0);
                    using (generator.StartAt(startPos, GeneratorDirection.Forward, true))
                    {
                        for (int i = (startPos.Offset == 0) ? startPos.Index : startPos.Index + 1; i < itemsControl.Items.Count; i++)
                        {
                            // Get or create the child
                            bool           newlyRealized;
                            TreeViewExItem child = generator.GenerateNext(out newlyRealized) as TreeViewExItem;
                            if (newlyRealized)
                            {
                                // Figure out if we need to insert the child at the end or somewhere in the middle
                                AddOrInsertItemToInternalChildren(i, child);
                                child.ParentTreeView = treeView ?? treeViewItem.ParentTreeView;
                                generator.PrepareItemContainer(child);
                            }

                            child.Measure(new Size(double.MaxValue, double.MaxValue));
                            // now get the real height
                            double height = child.DesiredSize.Height;
                            // add real height to current position
                            currentY += height;
                            // save the maximum needed width
                            maxWidth = Math.Max(maxWidth, child.DesiredSize.Width);
                        }
                    }
                }
            }

            Extent   = new Size(maxWidth, currentY);
            Viewport = availableSize;

            return(Extent);
        }
コード例 #6
0
        public void ChangeWithMaxItems()
        {
            var target = new SizesCache();
            target.AddOrChange(0, 1);
            target.AddOrChange(0, 1);
            target.AddOrChange(0, 3);
            target.AddOrChange(0, 3);
            target.AddOrChange(0, 5);
            target.AddOrChange(0, 7);
            target.AddOrChange(0, 7);
            target.AddOrChange(0, 9);
            target.AddOrChange(0, 9);
            target.AddOrChange(0, 11);
            target.AddOrChange(0, 11);
            target.AddOrChange(0, 11);

            Assert.AreEqual(11, target.GetEstimate(0));
        }