Tips for hit test in Silverlight

Source: Internet
Author: User

In the Silverlight process designer, hit tests were used twice. IsHitTestVisible and FindElementsInHostCoordinates are mainly used.

IsHitTestVisible is a property of the UIElement class, and is used to determine whether a click is effective. FindElementsInHostCoordinates is the VisualTreeHelper method used to find the UIElement object in a vertex or region.

1. Use of IsHitTestVisible

Scenario: during the migration between two links, I tried to dynamically draw a line to display the migration location. In the MouseLeftButtonDown event, record the migration start point. Get the migration endpoint in the MouseLeftButtonUp event and add a one-day migration dynamically. During the MouseMove process, I dynamically display a straight line to show the migration position.

Problem: The MouseLeftButtonUp event can be triggered at the end.

This problem is puzzling to me. Later, the expert asked for leave, who knew that the problem was that when MouseMove, He dynamically drew a straight line. The MouseLeftButtonUp event is not triggered on the link, but on that line. Because the mouse is very small, sometimes a pixel difference is not in the straight line and falls on the link. At this time, the event can be triggered normally. The solution is to assign the value of the IsHitTestVisible attribute to false.

2. FindElementsInHostCoordinates

Problem: The selection and insertion of migration points are all completed in the migration line by clicking the mouse. The migration line is too small and it is very labor-intensive to select a migration.

After finding the materials, I found that Silverlight also had a hit test. I added a hit test judgment on the parent node of the migration money to solve the problem. The sample code is as follows:

Code Rect clickArea = new Rect (e. GetPosition (null). X-3, e. GetPosition (null). Y-3, 6, 6 );
IEnumerable <UIElement> list = VisualTreeHelper. FindElementsInHostCoordinates (clickArea, this). Where (f =>{ return f is GraphicTransition ;});
If (list. Count ()> 0)
{
(List. First () as GraphicTransition). MouseLeftDown (e );
Return;
}

 

FindElementsInHostCoordinates () has two overload Methods: one is the test point hit and the other is the test area hit. In the code, I will test a click area to see if the migration is selected. This makes it much easier.

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.