이 코드를 사용하면 다른 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;
}
}
}