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

Magesh Sankar 31 Reputation points
2020-05-27T07:24:05.38+00:00

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

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

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,762 questions
0 comments No comments
{count} votes

Accepted answer
  1. gekka 8,856 Reputation points MVP
    2020-05-28T08:37:15.223+00:00

    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>");
    
        }
    }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.