Silverlight 2.0 hit test (hittest)

Source: Internet
Author: User
Tags silverlight

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.