我有以下网格:
<Grid x:Name="grd" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Height="100" >
<Button x:Name="but" OverridesDefaultStyle="True" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" >
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Bd" Style="{StaticResource MatrixStyle}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Height="50" />
</ControlTemplate>
</Button.Template>
</Button>
<Border BorderBrush="Black" BorderThickness="1" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Border BorderBrush="Black" BorderThickness="1" Width="50" Height="50" HorizontalAlignment="Right" VerticalAlignment="Top"/>
<Border BorderBrush="Black" BorderThickness="1" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
<Border BorderBrush="Black" BorderThickness="1" Width="50" Height="50" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
</Grid>
此父网格附加了MouseDown事件。如果我保留4个边框并删除按钮,则此事件会被触发。但如果我只保留按钮并删除其他边框,则此事件不会被触发。可能是这个问题的可能原因是什么?
答案 0 :(得分:2)
Button处理MouseDown事件并“消耗”它(即它通过为MouseDown事件设置“e.Handled = true”来防止它进一步向上路由到它的父节点)。因此,它无法到达网格控件。
话虽如此,您可能希望处理PreviewMouseDown事件。这个在它到达Button之前首先在Grid中被触发,所以你肯定能够在Grid上处理它。