本文整理汇总了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;
}
示例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();
}
}