有人可以向我解释为什么MouseDown
事件在这个简单的例子中没有达到ScrollViewer
吗?
<Window x:Class="MouseDownTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ScrollViewer Name="scrollViewer" Background="Green" MouseDown="ScrollViewer_MouseDown" PreviewMouseDown="ScrollViewer_PreviewMouseDown">
<Canvas Name="canvas" Background="Beige" MouseDown="Canvas_MouseDown" PreviewMouseDown="Canvas_PreviewMouseDown">
</Canvas>
</ScrollViewer>
</Grid>
</Window>
代码背后的代码:
using System;
using System.Windows;
using System.Windows.Input;
namespace MouseDownTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
scrollViewer.AddHandler(MouseDownEvent, new RoutedEventHandler(ScrollViewer_Test));
}
private void ScrollViewer_Test(object sender, RoutedEventArgs routedEventArgs)
{
Console.WriteLine("ScrollViewer_Test");
}
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Canvas_MouseDown");
}
private void Canvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Canvas_PreviewMouseDown");
}
private void ScrollViewer_MouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("ScrollViewer_MouseDown");
}
private void ScrollViewer_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("ScrollViewer_PreviewMouseDown");
}
}
}
单击画布时的输出
ScrollViewer_PreviewMouseDown
Canvas_PreviewMouseDown
Canvas_MouseDown
为什么省略ScrollViewer_MouseDown
?我已经查看了this article但是那里给出的解决方案没有帮助(或者我做错了)。
答案 0 :(得分:1)
您可能需要在Focusable="False"
上设置ScrollViewer
以允许鼠标事件通过
<ScrollViewer Name="scrollViewer" Focusable="False" Background="Green" />
<强>输出:强>
ScrollViewer_PreviewMouseDown
Canvas_PreviewMouseDown
Canvas_MouseDown
ScrollViewer_MouseDown