UIElement.IsHitTestVisible屬性可以設置WPF元素是否響應點擊等交互事件,利用該屬性可實現禁止選中DataGrid行的效果。
參考資料:禁用在WPF DataGrid中的選擇
方法1:屏蔽DataGrid點擊效果
該方法禁用了DataGrid所有交互事件,包含行滾動。
<DataGrid IsHitTestVisible="False"> </DataGrid>
方法2:屏蔽DataGridRow點擊效果
該方法禁用了DataGridRow所有交互事件,對於需要在DataGrid上右鍵彈出菜單以及行滾動比較有用,也是推薦的方法。
<DataGrid> <DataGrid.RowStyle> <!-- 重寫行樣式:禁止交互 --> <Style TargetType="DataGridRow"> <Setter Property="IsHitTestVisible" Value="False"/> </Style> </DataGrid.RowStyle> </DataGrid>