我有一个自定义的控件和结构,因为矩形被放置在画布中,我需要在触摸移动事件中,在画布上触摸移动(比如触摸选择或触摸第一个元素,将手指移动到最后一个元素)时获得相应的子元素源(矩形)。
上述情况在鼠标移动事件中工作正常,当鼠标移到它上面时,我可以得到相应的子元素引用。
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); ;
}
}
触摸移动时
尝试使用PreviewStylusMove或StylusMove代替TouchMove。