Search code examples
c#wpftouch

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 or touch the first element move the finger to last element) 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


Solution

  • Try using PreviewStylusMove or StylusMove instead of TouchMove.