单独设置 IsHitTestVisible 不会使其工作。
如果“背景”设置为“无”,则元素不可单击。要使其可点击(适用于网格、堆栈面板等),请将背景设置为#00000000。这是一项旨在防止用户点击幽灵按钮的功能。但是,为其指定颜色将使其可点击。
尝试在网格上设置 IsHitTestVisible = true
在我的固定情况下,对于中间只有一个按钮的 3x3 网格,我在 xaml 中为空行和空列添加了 PreviewMouseDown 事件:
<DataGridCell Grid.Row="0" PreviewMouseDown="EmptyAreaPreviewMouseDown"/>
<DataGridCell Grid.Row="2" PreviewMouseDown="EmptyAreaPreviewMouseDown"/>
<DataGridCell Grid.Column="0" PreviewMouseDown="EmptyAreaPreviewMouseDown"/>
<DataGridCell Grid.Row="2" PreviewMouseDown="EmptyAreaPreviewMouseDown"/>
在代码隐藏中:
private void EmptyAreaPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
// do here empty area work
}
希望它对将来的人有所帮助。