Click here to Skip to main content
13,346,487 members (66,135 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 11 Aug 2010

Get Mouse Click relative Position

Rate this:
Please Sign up or sign in to vote.
If you need to get the position the mouse was clicked, in relation to a specific UI Element, this might help
Today, I was faced with a requirement where I had to respond to the user clicking on a cell in a manually built grid. Gird your loins - the cell itself was a label in a border in a grid in a stack panel in a grid on the page. Because of the way this particular label was displayed, there was white space to the left of the actual text.

My layout produced some unexpected issues. I tried setting the mouse-up handler for the label, and that didn't work for white space in the cell. The same for the border. Finally, I put the handler in the grid for the entire row, and while the white space problem was solved, the mouse up event was fired for anything in the row that wasn'rt already handled by another handler (buttons).

So in my handler, I do this:

private void FMMetric_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    // make sure the event came from a grid element
    if (sender is Grid)
        // get the grid
        Grid element = sender as Grid;
        // get the first child (if possible). It *should* be a border
        if (element.Children != null && 
            element.Children.Count >= 1 && 
            element.Children[0] is Border)
            Border firstChild = element.Children[0] as Border;
            // now see where the user clicked the mouse in relation to the 
            // discovered border
            Point point = e.GetSafePosition(firstChild);
            // if the X value is <= the actual rendered width of the element, 
            // we can process the mouse click and navigate to the appropriate 
            // page.
            if (point.X <= firstChild.ActualWidth)
                // ... put code here ...

Of course the example above is specific to my code, but you should be able to see the technique.

You can use this approach to find the mouse position in relation to any given UI element on the page.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

John Simmons / outlaw programmer
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 Thank you for sharing this tip Pin
linuxjr11-Aug-10 7:00
memberlinuxjr11-Aug-10 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 12 Aug 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid