选择全部按钮WPF DataGrid

时间:2010-05-12 10:30:43

标签: wpf select datagrid user-controls

我想知道是否有禁用WPF DataGrid顶角的select all选项....这只会在我将一个UserControl添加到WPF中的fixeddocument时出现。

提前致谢,

Ú

6 个答案:

答案 0 :(得分:30)

HeadersVisibility中有一个属性DataGrid。它有四个值 - AllColumnRowNone

使用HeadersVisibility = All,您将获得SelectAll按钮。

使用HeadersVisibility = Column,您只能获得列。不是SelectAll Button或Row Headers来选择一个完整的行。

使用HeadersVisibility = Row,您将只获得Row标题以选择整行。不是SelectAll按钮或列。

使用HeadersVisibility = None,你什么也得不到。所有标题都将被隐藏。

我希望这会对你有所帮助。

答案 1 :(得分:6)

使用Snoop分析我放在一起的测试应用程序的Visual Tree后,我使用DataGrid_Loaded事件想出了这个解决方案):

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解决方案,但这首先想到的是,它似乎运行得很好(我显然也没有做任何异常处理)。

注意:我没有玩过禁用/重新启用DataGrid以确保禁用全部选择按钮 。如果它没有保持禁用状态,那么您可能还想要挂钩DataGrid_IsEnabledChanged事件。

希望这有帮助!!

答案 2 :(得分:3)

将命令绑定添加到SelectAll命令并在CanExecute中返回false以禁用selectall按钮。

请参阅:Event for Select All: WPF Datagrid

答案 3 :(得分:0)

我会更改DataGrid的Control Templat e。 需要在模板内禁用此按钮。 这是DataGrid ControlTemplate

  <ControlTemplate TargetType="{x:Type DataGrid}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"
                            Padding="{TemplateBinding Padding}"
                            SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="DG_ScrollViewer"
                                      Focusable="false">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>
                                        <Button Command="{x:Static DataGrid.SelectAllCommand}"
                                                Focusable="false"
                                                Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}"
                                                Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                                Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                        <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter"
                                                                        Grid.Column="1"
                                                                        Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
                                                                CanContentScroll="{TemplateBinding CanContentScroll}"
                                                                Grid.ColumnSpan="2"
                                                                Grid.Row="1" />
                                        <ScrollBar x:Name="PART_VerticalScrollBar"
                                                   Grid.Column="2"
                                                   Maximum="{TemplateBinding ScrollableHeight}"
                                                   Orientation="Vertical"
                                                   Grid.Row="1"
                                                   Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                                                   Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
                                                   ViewportSize="{TemplateBinding ViewportHeight}" />
                                        <Grid Grid.Column="1"
                                              Grid.Row="2">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>
                                            <ScrollBar x:Name="PART_HorizontalScrollBar"
                                                       Grid.Column="1"
                                                       Maximum="{TemplateBinding ScrollableWidth}"
                                                       Orientation="Horizontal"
                                                       Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                                                       Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
                                                       ViewportSize="{TemplateBinding ViewportWidth}" />
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>

手动停用该按钮并将此ControlTemplate分配给您的DataGrid

答案 4 :(得分:0)

如果您不需要在DataGrid中进行扩展选择(即切换到单个单元格选择),则可以设置:

 <DataGrid SelectionMode="Single">

它还会禁用左上角的SelectAll按钮。

答案 5 :(得分:0)

基于this answer,您可以保留标题和选择模式。

在用户控件的资源内放置:

<Style x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}"
       TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <!-- an empty ControlTemplate is fine -->
            <ControlTemplate TargetType="{x:Type Button}" />
        </Setter.Value>
    </Setter>
</Style>

Result

还有更多工作要做,您可以添加一个工具提示来帮助用户发现 Ctrl Shift

<Style x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}"
       TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <!-- an empty ControlTemplate is fine -->
            <ControlTemplate TargetType="{x:Type Button}">
                <DockPanel HorizontalAlignment="Center"
                           IsHitTestVisible="False"
                           VerticalAlignment="Center">
                    <TextBlock FontSize="18"
                               FontWeight="ExtraBlack"
                               Text="ⓘ"
                               TextAlignment="Center"
                               ToolTip="{StaticResource DataGridHowTo}" />
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Result 2

有关行标题中的复选框,请参见this post