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)
{
if (sender is Grid)
{
Grid element = sender as Grid;
if (element.Children != null &&
element.Children.Count >= 1 &&
element.Children[0] is Border)
{
Border firstChild = element.Children[0] as Border;
Point point = e.GetSafePosition(firstChild);
if (point.X <= firstChild.ActualWidth)
{
}
}
}
}
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.
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.