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.