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 6126229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:20:46+00:00 2026-05-23T16:20:46+00:00

I am trying to write a WPF application where you can draw circles on

  • 0

I am trying to write a WPF application where you can draw circles on a window by double clicking it. So far I have this code:

public class ShapeAdorner : Adorner
{
    private readonly Ellipse _circle;

    public ShapeAdorner(UIElement adornedElement, Point circleCenter)
        : base(adornedElement)
    {
        _circle = new Ellipse
        {
            Width = 10,
            Height = 10,
            Stroke = Brushes.Black,
            StrokeThickness = 1.5
        };
        _circle.Margin =
            new Thickness(left: circleCenter.X, top: circleCenter.Y, right: 0, bottom: 0);
        base.AddVisualChild(_circle);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        _circle.Arrange(new Rect(finalSize));
        return finalSize;
    }

    protected override Size MeasureOverride(Size constraint)
    {
        _circle.Measure(constraint);
        return constraint;
    }

    protected override Visual GetVisualChild(int index)
    {
        return _circle;
    }

    protected override int VisualChildrenCount
    {
        get { return 1; }
    }
}

Here’s the client code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(myLabel);
        adornerLayer.Add(new ShapeAdorner(adornedElement: myLabel, circleCenter: e.GetPosition(myLabel)));
    } 
}

The circles are supposed to be centered at the point where you double click the window; however, the circles drawn by the code above are centered below and to the right of “the double click point”. How can this be fixed?

EDIT: myLabel has Height=350 and Width=525. Let’s say that I double click the point (X,Y); then the circle gets plotted at ((350+X)/2,(525+Y)/2).

EDIT 2: Just for completeness, here’s the .xaml file:

<Window x:Class="Adorners.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Adorners project" Height="350" Width="525" MouseDoubleClick="Window_MouseDoubleClick">
    <Grid>
        <Label Name="myLabel" Content="my label" Background="Red"></Label>
    </Grid>
</Window>
  • 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-23T16:20:47+00:00Added an answer on May 23, 2026 at 4:20 pm

    The previous answers are correct. However, the main problem was that I had omitted these two lines:


    _circle.HorizontalAlignment = HorizontalAlignment.Left;
    _circle.VerticalAlignment = VerticalAlignment.Top;

    The default value Stretch caused a huge offset error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF Application that I have been trying to write in the
I'm trying to write validation rules for my data objects in a WPF application
I'm trying to have a WPF canvas with rounded rectangles on that I can
I'm trying to write a simple WPF application in C#. Coming from a primarily
I have a wpf application with a simple window that shows a text in
I have a WPF application that mostly follows MVVM, which I am trying to
I'm trying to write a WPF application for displaying images from a selection. I
I'm trying to write a WPF application that will update a set of text
So i'm new to WPF, and i'm trying to write an application that displays
I am trying to create a WPF application where I have a StackPanel that

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.