Silverlight中怎么获取Canvas子控件的位置坐标
Silverlight中怎么获取Canvas子控件的位置坐标
[解决办法]
Canvas.GetLeft(ChildElement)
Canvas.GetTop(ChildElement)
[解决办法]
foreach (var item in MyCanvas.Children)
{
var box=item as BoxControl;
if (box.PtreeNodeId != null)
{
foreach (var pItem in MyCanvas.Children)
{
var pBox = pItem as BoxControl;
#region 计算连线两点的坐标
if (pBox.TreeNodeId == box.PtreeNodeId)
{
pMarginToTop = Canvas.GetTop(pItem);
marginToTop = Canvas.GetTop(item);
if (pMarginToTop > marginToTop)
{
boxX1 = Canvas.GetLeft(item) + box.Width / 2;
boxY1 = Canvas.GetTop(item);
pBoxX2 = Canvas.GetLeft(pItem) + pBox.Width / 2;
pBoxY2 = Canvas.GetTop(pItem) + pBox.Height;
}
else if (pMarginToTop < marginToTop)
{
boxX1 = Canvas.GetLeft(item) + box.Width / 2;
boxY1 = Canvas.GetTop(item) + box.Height;
pBoxX2 = Canvas.GetLeft(pItem) + pBox.Width / 2;
pBoxY2 = Canvas.GetTop(pItem);
}
else
{
pMarginToLeft = Canvas.GetLeft(pItem);
marginToLeft = Canvas.GetLeft(item);
if (pMarginToLeft > marginToLeft)
{
boxX1 = Canvas.GetLeft(item) + box.Width;
boxY1 = Canvas.GetTop(item) + box.Height / 2;
pBoxX2 = Canvas.GetLeft(pItem);
pBoxY2 = Canvas.GetTop(pItem) + pBox.Height / 2;
}
else if (pMarginToLeft < marginToLeft)
{
boxX1 = Canvas.GetLeft(item);
boxY1 = Canvas.GetTop(item) + box.Height / 2;
pBoxX2 = Canvas.GetLeft(pItem) + pBox.Width;
pBoxY2 = Canvas.GetTop(pItem) + pBox.Height / 2;
}
}
}
#endregion