Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5937011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:28:46+00:00 2026-05-22T15:28:46+00:00

How do I get a WPF Canvas object to render a TextBlock and a

  • 0

How do I get a WPF Canvas object to render a TextBlock and a Polygon in the exact same position? I’m trying to overlay 2D labels onto a 3D scene, but while my Polygon backgrounds end in the right places, the TextBlocks don’t…

You can see the results here.

(I know it’d be better to use Border objects instead of Polygons, but they don’t position correctly either!)

Here’s the code I’m using to try and do this:

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);
}

I would be most grateful if you could help me get my Canvas under control, thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:28:47+00:00Added an answer on May 22, 2026 at 3:28 pm

    Aha, the answer is that you need to specify Right and Top as well as Left and Bottom to make sure it’s positioned exactly where you want it…

    Canvas.SetLeft(border, middle.X - border.Width / 2);
    Canvas.SetRight(border, middle.X + border.Width / 2);
    Canvas.SetBottom(border, middle.Y + border.Height / 2);
    Canvas.SetTop(border, middle.Y - border.Height / 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get WPF validation to work within the MVVM pattern. In my
Im trying to get my head wrapped around the WPF model. I have a
I'm trying to have a WPF canvas with rounded rectangles on that I can
I'm trying to print a WPF canvas over multiple pages. I'm perfectly happy with
I try to get all WPF window controls collections. In other words i try
What do I need to do to get my WPF DataGrid control to bind
Is there a way with WPF to get an array of elements under the
I cannot get a two-way bind in WPF to work. I have a string
In a nested WPF ToolBarPanel-ToolBar-Menu we want to get rid of the grip handle
How do you get an image to be reflected in wpf without using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.