如何禁用DataGrid的“全选”按钮 [英] How to disable the 'Select All' button of a DataGrid

查看:90
本文介绍了如何禁用DataGrid的“全选”按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以禁用WPF数据网格左上角的全选按钮?

Is it possible to disable the "Select all" button in the upper left corner of the WPF DataGrid?

推荐答案

之后使用 Snoop 分析我放在一起的测试应用程序的可视树,我使用DataGrid_Loaded提出了此解决方案事件):

After using Snoop to analyze the Visual Tree of a test app I put together, I came up with this solution using the DataGrid_Loaded event):

private void TheGrid_Loaded(object sender, RoutedEventArgs e) {
    var dataGrid = (DataGrid)sender;
    var border = (Border)VisualTreeHelper.GetChild(dataGrid, 0);
    var scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
    var grid = (Grid)VisualTreeHelper.GetChild(scrollViewer, 0);
    var button = (Button)VisualTreeHelper.GetChild(grid, 0);
    button.IsEnabled = false;
}

可能只有一个更优雅的XAML解决方案,但这是我首先想到的,它似乎已经足够好工作了(我显然也没有进行任何异常处理)。

There may be a more elegant XAML only solution out there, but this is what came to mind first, and it seems to work well enough (I'm obviously not doing any Exception handling either).

注意:我没有玩过禁用/重新启用DataGrid的操作确保保持的全选按钮已禁用。如果它没有保持禁用状态,那么您可能还希望加入DataGrid_IsEnabledChanged事件。

Note: I haven't played around with disabling/re-enabling the DataGrid to make sure that the select all button stays disabled. If it doesn't stay disabled, then you may want to also hook into the DataGrid_IsEnabledChanged event.

希望这会有所帮助!

这篇关于如何禁用DataGrid的“全选”按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆