For an object's Click event, we can capture it by clicking, MouseLeftButtonUp, MouseLeftButtonDown, but if there are dozens of or even hundreds of of these objects that need to be judged if they are clicked, you may feel uncomfortable, Because you want to add the same number of Button_Click or MouseDown, MouseUp event handlers, not to mention overlapping controls (two controls may even overlap).
Silverlight 2.0 has been improved in this regard by introducing the hittest approach to WPF. It's easier to use. For example, we have a canvas called layoutroot (canvas root element), so we can use the following code:
Ienumerable<uielement> elements=layoutroot.hittest (mousept);
foreach (uielement element in elements)
{
FrameworkElement Fe=element as FrameworkElement;
...
}
The mousept here is the current coordinates of the mouse, I think the discerning eye has already seen, this code is written in the MouseDown or MouseUp event handler, you can write it directly in the root canvas MouseDown or MouseUp event, This ensures that all mouse-click events are captured and judged, and, of course, if there are other requirements, they can be written separately in the MouseDown or MouseUp of a particular container element.
In order to facilitate a better understanding of hittest, I wrote a small sample, as long as your mouse click on the location of the rectangle, the following TextBlock will be the corresponding rectangle name display. The code is compiled through the VS2008 and expression Blend 2.5 March preview.
This article supporting source code