このコードを使用して、別のUIElementに対するUIElementsの位置と境界を取得できます。コードはthis postから取得されています。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
public static class UIHelper
{
public static Boolean GetUIElementCornersRelativTo(UIElement Base,
UIElement RelativeTo,
ref Point TopLeft,
ref Point BottomLeft,
ref Point BottomRight,
ref Point TopRight,
ref String Message)
{
try
{
if (Base == null)
{
throw new Exception("Base UIElement is null");
}
if (RelativeTo == null)
{
throw new Exception("RelativTo UIElement is null");
}
TopLeft = Base.TranslatePoint(new Point(0, 0), RelativeTo);
BottomLeft = Base.TranslatePoint(new Point(0, Base.RenderSize.Height), RelativeTo);
BottomRight = Base.TranslatePoint(new Point(Base.RenderSize.Width, Base.RenderSize.Height), RelativeTo);
TopRight = Base.TranslatePoint(new Point(Base.RenderSize.Width, 0), RelativeTo);
Message = "OK";
return true;
}
catch (Exception ex)
{
Message = ex.Message;
return false;
}
}
public static Boolean GetPointRelativTo(UIElement Base,
UIElement RelativeTo,
Point ToProjectPoint,
ref Point Result,
ref String Message)
{
try
{
if (Base == null)
{
throw new Exception("Base UIElement is null");
}
if (RelativeTo == null)
{
throw new Exception("RelativTo UIElement is null");
}
if (ToProjectPoint == null)
{
throw new Exception("To project point is null");
}
Result = Base.TranslatePoint(ToProjectPoint, RelativeTo);
Message = "OK";
return true;
}
catch (Exception ex)
{
Message = ex.Message;
return false;
}
}
}