如何獲取WPF Canvas對象在完全相同的位置呈現TextBlock和Polygon?我想2D標籤覆蓋到3D場景,但同時我的多邊形背景在正確的地方結束,該的TextBlocks不...如何讓WPF Canvas將TextBlock和Polygon放在同一個地方?
(我知道這會是更好的使用邊界對象,而不是多邊形,但他們不正確的任一位置)
下面是我使用的嘗試做這樣的代碼:
foreach (Block block in blocks)
{
GeneralTransform3DTo2D transform = block.Top.TransformToAncestor(viewport);
GeometryModel3D model = block.Top.Content as GeometryModel3D;
MeshGeometry3D geometry = model.Geometry as MeshGeometry3D;
Point corner1 = transform.Transform(geometry.Positions[geometry.TriangleIndices[0]]);
Point corner2 = transform.Transform(geometry.Positions[geometry.TriangleIndices[1]]);
Point middle = new Point(corner1.X/2 + corner2.X/2, corner1.Y/2 + corner2.Y/2);
Polygon polygon = new Polygon();
polygon.Stroke = Brushes.DimGray;
polygon.Fill = Brushes.LightGray;
polygon.Points.Add(new Point(middle.X - 15, middle.Y - 15));
polygon.Points.Add(new Point(middle.X + 15, middle.Y - 15));
polygon.Points.Add(new Point(middle.X + 15, middle.Y + 15));
polygon.Points.Add(new Point(middle.X - 15, middle.Y + 15));
labelHolder.Children.Add(polygon);
TextBlock text = new TextBlock();
text.Text = block.Text;
text.Width = 30;
text.Height = 30;
text.TextAlignment = TextAlignment.Center;
text.FontSize = 11;
Canvas.SetLeft(text, middle.X - 15);
Canvas.SetBottom(text, middle.Y - 15);
labelHolder.Children.Add(text);
}
我將不勝感激,如果你能幫助我可以控制我的帆布,謝謝!