public ScrollViewer() { // put the scroll bars above the presenter and add them to the grid canvas foreach (var bar in scrollBars) { bar.Measure(Vector3.Zero); // set is measure valid to true VisualChildrenCollection.Add(bar); SetVisualParent(bar, this); } CanBeHitByUser = TouchScrollingEnabled; ClipToBounds = true; }
/// <summary> /// Action to perform when a logical child is added. /// </summary> /// <param name="newElement">The element that has been added</param> /// <param name="index">The index in the collection where the child has been added</param> protected virtual void OnLogicalChildAdded(UIElement newElement, int index) { if (newElement == null) { throw new InvalidOperationException("Cannot add a null UIElement to the children list."); } SetParent(newElement, this); SetVisualParent(newElement, this); VisualChildrenCollection.Sort(PanelChildrenSorter); if (Children.Count > childrenArrangeWorldMatrix.Length) { childrenArrangeWorldMatrix = new Matrix[2 * Children.Count]; } }
private void UpdateAndArrangeVisibleChildren() { var axis = (int)Orientation; // cache the initial list of children cachedVisibleChildren.Clear(); foreach (var child in visibleChildren) { cachedVisibleChildren.Add(child); } // reset the list visibleChildren.Clear(); // remove all the current visual children while (VisualChildrenCollection.Count > 0) { SetVisualParent(VisualChildrenCollection[0], null); } // determine first element index and size var elementIndex = (int)Math.Floor(scrollPosition); var firstChildSize = GetSafeChildSize(elementIndex, axis); // create the next visual children collection to display var currentSize = -(scrollPosition - elementIndex) * firstChildSize; while (elementIndex < Children.Count && currentSize <= Viewport[axis]) { currentSize += GetSafeChildSize(elementIndex, axis); visibleChildren.Add(Children[elementIndex]); SetVisualParent(Children[elementIndex], this); ++elementIndex; } // reorder visual children by z-Order VisualChildrenCollection.Sort(PanelChildrenSorter); // re-arrange the children if they changed if (visibleChildren.Count > 0) { var shouldRearrangeChildren = cachedVisibleChildren.Count == 0 || cachedVisibleChildren.Count != visibleChildren.Count; // determine if the two list are equals if (!shouldRearrangeChildren) { for (int i = 0; i < visibleChildren.Count; i++) { if (cachedVisibleChildren[i] != visibleChildren[i]) { shouldRearrangeChildren = true; break; } } } if (shouldRearrangeChildren) { ArrangeChildren(); } } }