需要在触摸移动事件中获取子元素(e.Source)。

问题描述 投票:3回答:1

我有一个自定义的控件和结构,因为矩形被放置在画布中,我需要在触摸移动事件中,在画布上触摸移动(比如触摸选择或触摸第一个元素,将手指移动到最后一个元素)时获得相应的子元素源(矩形)。

上述情况在鼠标移动事件中工作正常,当鼠标移到它上面时,我可以得到相应的子元素引用。

Xaml代码

 <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); ;

    }
}

触摸移动时

触摸移动时,我总是只能得到第一个元素的引用。enter image description here

鼠标移动时鼠标移动时,我可以得到所有相应的子代的参照物enter image description here

c# wpf touch
1个回答
2
投票

尝试使用PreviewStylusMove或StylusMove代替TouchMove。

© www.soinside.com 2019 - 2024. All rights reserved.