当前位置: 首页>>代码示例>>C#>>正文


C# FrameworkElement.GetBoundsRelativeTo方法代码示例

本文整理汇总了C#中FrameworkElement.GetBoundsRelativeTo方法的典型用法代码示例。如果您正苦于以下问题:C# FrameworkElement.GetBoundsRelativeTo方法的具体用法?C# FrameworkElement.GetBoundsRelativeTo怎么用?C# FrameworkElement.GetBoundsRelativeTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在FrameworkElement的用法示例。


在下文中一共展示了FrameworkElement.GetBoundsRelativeTo方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: IsInView

        /// <summary>
        /// Determine whether an element is currently in the view of the
        /// ScrollViewer.
        /// </summary>
        /// <param name="viewer">The ScrollViewer.</param>
        /// <param name="element">The element.</param>
        /// <returns>
        /// A value indicating whether the element is currently in the view of
        /// the ScrollViewer.
        /// </returns>
        private static bool IsInView(ScrollViewer viewer, FrameworkElement element)
        {
            Assert.IsNotNull(viewer, "viewer should not be null!");
            Assert.IsNotNull(element, "element should not be null!");

            Rect? itemBounds = element.GetBoundsRelativeTo(viewer);
            if (itemBounds == null)
            {
                return false;
            }

            double viewBottom = viewer.ViewportHeight;
            double viewRight = viewer.ViewportWidth;

            return
                itemBounds.Value.Top >= 0 && itemBounds.Value.Top <= viewBottom &&
                itemBounds.Value.Bottom >= 0 && itemBounds.Value.Bottom <= viewBottom &&
                itemBounds.Value.Left >= 0 && itemBounds.Value.Left <= viewRight &&
                itemBounds.Value.Right >= 0 && itemBounds.Value.Right <= viewRight;
        }
开发者ID:shijiaxing,项目名称:SilverlightToolkit,代码行数:30,代码来源:ScrollViewerExtensionsTest.cs

示例2: ScrollIntoView

        /// <summary>
        /// Scroll the desired element into the ScrollViewer's viewport.
        /// </summary>
        /// <param name="viewer">The ScrollViewer.</param>
        /// <param name="element">The element to scroll into view.</param>
        /// <param name="horizontalMargin">The margin to add on the left or right.
        /// </param>
        /// <param name="verticalMargin">The margin to add on the top or bottom.
        /// </param>
        /// <param name="duration">The duration of the animation.</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// <paramref name="viewer" /> is null.
        /// </exception>
        /// <exception cref="T:System.ArgumentNullException">
        /// <paramref name="element" /> is null.
        /// </exception>
        public static void ScrollIntoView(this ScrollViewer viewer, FrameworkElement element, double horizontalMargin, double verticalMargin, Duration duration)
        {
            if (viewer == null)
            {
                throw new ArgumentNullException("viewer");
            }
            else if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            // Get the position of the element relative to the ScrollHost
            Rect? itemRect = element.GetBoundsRelativeTo(viewer);
            if (itemRect == null)
            {
                return;
            }

            // Scroll vertically
            double verticalOffset = viewer.VerticalOffset;
            double verticalDelta = 0;
            double hostBottom = viewer.ViewportHeight;
            double itemBottom = itemRect.Value.Bottom + verticalMargin;
            if (hostBottom < itemBottom)
            {
                verticalDelta = itemBottom - hostBottom;
                verticalOffset += verticalDelta;
            }
            double itemTop = itemRect.Value.Top - verticalMargin;
            if (itemTop - verticalDelta < 0)
            {
                verticalOffset -= verticalDelta - itemTop;
            }

            // Scroll horizontally
            double horizontalOffset = viewer.HorizontalOffset;
            double horizontalDelta = 0;
            double hostRight = viewer.ViewportWidth;
            double itemRight = itemRect.Value.Right + horizontalMargin;
            if (hostRight < itemRight)
            {
                horizontalDelta = itemRight - hostRight;
                horizontalOffset += horizontalDelta;
            }
            double itemLeft = itemRect.Value.Left - horizontalMargin;
            if (itemLeft - horizontalDelta < 0)
            {
                horizontalOffset -= horizontalDelta - itemLeft;
            }

            if (duration == TimeSpan.Zero)
            {
                viewer.ScrollToVerticalOffset(verticalOffset);
                viewer.ScrollToHorizontalOffset(horizontalOffset);
            }
            else
            {
                Storyboard storyboard = new Storyboard();
                SetVerticalOffset(viewer, viewer.VerticalOffset);
                SetHorizontalOffset(viewer, viewer.HorizontalOffset);

                DoubleAnimation verticalOffsetAnimation = new DoubleAnimation { To = verticalOffset, Duration = duration };
                DoubleAnimation horizontalOffsetAnimation = new DoubleAnimation { To = verticalOffset, Duration = duration };

                Storyboard.SetTarget(verticalOffsetAnimation, viewer);
                Storyboard.SetTarget(horizontalOffsetAnimation, viewer);
                Storyboard.SetTargetProperty(horizontalOffsetAnimation, new PropertyPath(ScrollViewerExtensions.HorizontalOffsetProperty));
                Storyboard.SetTargetProperty(verticalOffsetAnimation, new PropertyPath(ScrollViewerExtensions.VerticalOffsetProperty));

                storyboard.Children.Add(verticalOffsetAnimation);
                storyboard.Children.Add(horizontalOffsetAnimation);

                storyboard.Begin();
            }
        }
开发者ID:shijiaxing,项目名称:SilverlightToolkit,代码行数:91,代码来源:ScrollViewerExtensions.cs


注:本文中的FrameworkElement.GetBoundsRelativeTo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。