WPF禁止DataGrid行选中

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>
posted @ 2022-02-23 16:15  xhubobo  阅读(2981)  评论(0编辑  收藏  举报