question

MageshSankar-5062 avatar image
0 Votes"
MageshSankar-5062 asked gekka answered

Need to get child element(e.Source) on Touch Move event

I have an custom control and structure as Rectangles are placed inside the Canvas, I need to get the corresponding child element source(rectangle) while touch move(like touch selection) on the canvas in touch move event.


The above scenario working fine in Mouse Move event where I can get corresponding child reference when mouse over on it.

Xaml Code

  <Grid>
     <Canvas TouchMove="Canvas_TouchMove" MouseMove="Canvas_MouseMove" >
         <WrapPanel >
             <Rectangle Fill="Red" Name="Red" 
              Width="200" Height="200" 
              IsManipulationEnabled="true" />
             <Rectangle Fill="Blue" Name="Blue"
              Width="200" Height="200" 
              IsManipulationEnabled="true" />
             <Rectangle Fill="Yellow" Name="Yellow"
              Width="200" Height="200" 
              IsManipulationEnabled="true" />
             <Rectangle Fill="Green" Name="Green"
              Width="200" Height="200" 
              IsManipulationEnabled="true" />
             <Rectangle Fill="Pink" Name="Pink"
              Width="200" Height="200" 
              IsManipulationEnabled="true" />
             <Rectangle Fill="Red" Name="manRect5"
              Width="200" Height="200" 
              IsManipulationEnabled="true" />
         </WrapPanel>
           
     </Canvas>
 </Grid>


C#

   /// <summary>
 /// Interaction logic for MainWindow.xaml
 /// </summary>
 public partial class MainWindow : Window
 {
     public MainWindow()
     {
         InitializeComponent();
     }

     private void Canvas_TouchMove(object sender, TouchEventArgs e)
     {
         Console.WriteLine((e.Source as Rectangle).Name); ;
     }


     private void Canvas_MouseMove(object sender, MouseEventArgs e)
     {
         Console.WriteLine((e.Source as Rectangle).Name); ;

     }
 }

On TouchMove

On touch move I can get only first element reference always
![enter image description here][1]


On Mouse Move
On mouse move I can get all corresoponding child refference
![enter image description here][2]


windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

gekka avatar image
1 Vote"
gekka answered

Due to the fact that IsManipulation is set to true, manipulate operations are handled preferentially.
By change it to false, e.Source gets element under the touch pointer.

If you want to keep it true and get element under the touch pointer , you can find with HitTest.

private void Canvas_TouchMove(object sender, TouchEventArgs e)
{
    if (e.Source is FrameworkElement source)
    {
        var canvas = (Canvas)sender;
        var point = e.GetTouchPoint(canvas).Position;

        var hitresult = VisualTreeHelper.HitTest(canvas, point);
        var hit = hitresult?.VisualHit;
        if (hit != null)
        {
            FrameworkElement fe = hit as FrameworkElement;
            if (fe?.TemplatedParent != null)
            {
                hit = fe.TemplatedParent;
            }
        }

        if (hit is FrameworkElement element)
        {
            Console.WriteLine($"{DateTime.Now:HH:mm:ss}\t{source.Name}\t{element.Name}");
        }
    }
    else
    {
        Console.WriteLine("<NULL>");

    }
}
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.