计算边界多个元素silverlight

时间:2011-12-06 19:30:28

标签: c# silverlight-4.0

我有一个画布,在这个画布上有几个元素。我可以分别得到这些元素的界限。

如何用该数据计算所有元素的边界?

请查看此图形表示,希望澄清事情。

Graphical representation

1 个答案:

答案 0 :(得分:1)

将元素每个角的Point值拉入列表,然后获取min和max X和Y值

        List<Point> Points = new List<Point>();

        foreach (UIElement x in cvsMain.Children.Where(ui => ui.GetType() == typeof(Rectangle)))
        {
            // Obtain transform information based off element you need to find position within
            GeneralTransform gt = x.TransformToVisual(cvsMain);

            // Find the four corners of the element
            Points.Add(gt.Transform(new Point(0, 0)));
            Points.Add(gt.Transform(new Point((x as Rectangle).Width, 0)));
            Points.Add(gt.Transform(new Point(0, (x as Rectangle).Height)));
            Points.Add(gt.Transform(new Point((x as Rectangle).Width, (x as Rectangle).Height)));
        }

        Double Left = Points.Min(p => p.X);
        Double Right = Points.Max(p => p.X);
        Double Top = Points.Min(p => p.Y);
        Double Bottom = Points.Max(p => p.Y);

此网站显示如何查找每个角落的值 http://forums.silverlight.net/t/12160.aspx/1