我发现只有在包含Mouse.MouseDown
的单元格中单击鼠标时才会出现PreviewMouseLeftButtonDown
和UIElement
,例如:
XAML
<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WpfApplication5.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="300" Height="300">
<Grid Mouse.MouseDown="Grid_MouseDown" PreviewMouseLeftButtonDown="Grid_PreviewMouseLeftButtonDown">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Rectangle Grid.Column="2" Grid.RowSpan="2" Fill="Red"/>
</Grid>
</Window>
代码隐藏
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("PreviewMouseLeftButtonDown raised!");
}
private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("MouseDown raised!");
}
}
单击红色矩形时,只会将行写入Debug。如何才能使事件或至少其中一个事件被提升,网格中什么都不包含?
答案 0 :(得分:1)
我不知道问题的原因,但作为一种解决方法,为Grid
提供了一个背景,甚至是透明,似乎可以提升事件!
<Grid Background="Transparent" Mouse.MouseDown="Grid_MouseDown" PreviewMouseLeftButtonDown="Grid_PreviewMouseLeftButtonDown">