本文整理汇总了C#中FrameworkElement.TransformToVisual方法的典型用法代码示例。如果您正苦于以下问题:C# FrameworkElement.TransformToVisual方法的具体用法?C# FrameworkElement.TransformToVisual怎么用?C# FrameworkElement.TransformToVisual使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FrameworkElement
的用法示例。
在下文中一共展示了FrameworkElement.TransformToVisual方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCenterToCenterDelta
/// <summary>
/// Computes the delta between the centre of an element and its
/// container.
/// </summary>
/// <param name="element">The element to compare.</param>
/// <param name="container">The element to compare against.</param>
/// <returns>A point that represents the delta between the two centers.</returns>
static Point GetCenterToCenterDelta(FrameworkElement element, FrameworkElement container)
{
var elementCenter = new Point(element.ActualWidth / 2, element.ActualHeight / 2);
Point containerCenter;
#if WINDOWS_PHONE
// Need to special-case the frame to handle different orientations.
var frame = container as PhoneApplicationFrame;
if (frame != null)
{
// Switch width and height in landscape mode
containerCenter =
((frame.Orientation & PageOrientation.Landscape) == PageOrientation.Landscape) ?
new Point(container.ActualHeight / 2, container.ActualWidth / 2) :
new Point(container.ActualWidth / 2, container.ActualHeight / 2);
}
else
{
containerCenter = new Point(container.ActualWidth / 2, container.ActualHeight / 2);
}
#else
containerCenter = new Point(container.ActualWidth / 2, container.ActualHeight / 2);
#endif
#if WINDOWS_STORE
var transformedElementCenter = element.TransformToVisual(container).TransformPoint(elementCenter);
#elif WINDOWS_PHONE
var transformedElementCenter = element.TransformToVisual(container).Transform(elementCenter);
#endif
return new Point(containerCenter.X - transformedElementCenter.X, containerCenter.Y - transformedElementCenter.Y);
}
示例2: ScrollIntoView
internal void ScrollIntoView(FrameworkElement element)
{
// Get the ScrollHost
ScrollViewer scrollHost = ScrollHost;
if (scrollHost == null)
{
return;
}
// Get the position of the element relative to the ScrollHost
GeneralTransform transform = null;
try
{
transform = element.TransformToVisual(scrollHost);
}
catch (ArgumentException)
{
// Ignore failures when not in the visual tree
return;
}
Rect itemRect = new Rect(
transform.Transform(new Point()),
transform.Transform(new Point(element.ActualWidth, element.ActualHeight)));
// Scroll vertically
double verticalOffset = scrollHost.VerticalOffset;
double verticalDelta = 0;
double hostBottom = scrollHost.ViewportHeight;
double itemBottom = itemRect.Bottom;
if (hostBottom < itemBottom)
{
verticalDelta = itemBottom - hostBottom;
verticalOffset += verticalDelta;
}
double itemTop = itemRect.Top;
if (itemTop - verticalDelta < 0)
{
verticalOffset -= verticalDelta - itemTop;
}
scrollHost.ScrollToVerticalOffset(verticalOffset);
// Scroll horizontally
double horizontalOffset = scrollHost.HorizontalOffset;
double horizontalDelta = 0;
double hostRight = scrollHost.ViewportWidth;
double itemRight = itemRect.Right;
if (hostRight < itemRight)
{
horizontalDelta = itemRight - hostRight;
horizontalOffset += horizontalDelta;
}
double itemLeft = itemRect.Left;
if (itemLeft - horizontalDelta < 0)
{
horizontalOffset -= horizontalDelta - itemLeft;
}
scrollHost.ScrollToHorizontalOffset(horizontalOffset);
}
示例3: BoundsRelativeTo
public static Rect BoundsRelativeTo(FrameworkElement element, Visual relativeTo)
{
return element.TransformToVisual(relativeTo).TransformBounds(LayoutInformation.GetLayoutSlot(element));
}