Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get Y-Position out of the Rectangle Hashcode

everyone,
I have a linechart on a Canvas. I placed a rectangle over each data point and added a MouseEnter event.

private void SetDataPointLabels(Point pt)
    {
        
        Rectangle rec = new Rectangle();
        rec.Stroke = Brushes.Red;
        rec.StrokeThickness = 1;
        rec.Width = 5;
        rec.Height = 5;
        rec.MouseEnter += Rec_MouseEnter;
        Canvas.SetTop(rec, pt.Y - 2.5);
        Canvas.SetLeft(rec, pt.X - 2.5);
        ChartCanvas.Children.Add(rec);            
    }

I now need the y-position of the rectangle in the event method.

private void Rec_MouseEnter(object sender, MouseEventArgs e)
    {            
        Console.WriteLine(sender.GetHashCode());
    }

In the sender I found the Y position under VisualOffset.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Unfortunately, you can probably only get it via the hash code (sender.GetHashCode()) and I don’t know how.
Can anyone help me?
Thank you
Chris

>Solution :

The sender argument holds a reference to the Rectangle instance and can therefore be cast to the Rectangle type:

private void Rec_MouseEnter(object sender, MouseEventArgs e)
{            
    var rec = (Rectangle)sender;
    var y = Canvas.GetTop(rec);
    Debug.WriteLine(y);
}

Or just cast to UIElement if you want to use the event handler also for other element types:

var y = Canvas.GetTop((UIElement)sender);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading