获取WPF Datagrid中列的全选按钮

时间:2014-06-03 08:18:22

标签: c# wpf wpfdatagrid

我创建了一个WPF Datagrid,列定义如下:

<DataGrid.Columns>
<DataGridTemplateColumn Header="" MinWidth="32" Width="Auto" IsReadOnly="True">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                    <CheckBox IsChecked="{Binding Select, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" HorizontalContentAlignment="Stretch" ClickMode="Press"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="Package Name" Binding="{Binding PackageName}"  MinWidth="180" IsReadOnly="True" ElementStyle="{StaticResource ResourceKey=StyleDataGridText}"/>
                    <DataGridTextColumn Header="Version" Binding="{Binding Version}" Width="Auto" MinWidth="120" IsReadOnly="True" ElementStyle="{StaticResource ResourceKey=StyleDataGridText}"/>
                    <DataGridTextColumn Header="Release Date" Binding="{Binding ReleaseDate}" Width="Auto" MinWidth="120" IsReadOnly="True" ElementStyle="{StaticResource ResourceKey=StyleDataGridText}"/>
                    <DataGridTextColumn Header="Size" Binding="{Binding Size}" Width="Auto" MinWidth="10" IsReadOnly="True" ElementStyle="{StaticResource ResourceKey=StyleDataGridText}"/>
                    <DataGridTextColumn Header="Status" Binding="{Binding Status}" Width="Auto" MinWidth="80" IsReadOnly="True" ElementStyle="{StaticResource ResourceKey=StyleDataGridText}"/>
                    <DataGridHyperlinkColumn Header="Details" Binding="{Binding Details}" ContentBinding="{Binding Source={StaticResource ViewDetails}}" Width="Auto" MinWidth="80" IsReadOnly="True">
                        <DataGridHyperlinkColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <EventSetter Event="Hyperlink.Click" Handler="OnBaseHyperlinkClick"/>
                                    <Setter Property="HorizontalAlignment" Value="Left"/>
                                    <Setter Property="Margin" Value="5,2,0,0"/>
                            </Style>
                        </DataGridHyperlinkColumn.ElementStyle>
                    </DataGridHyperlinkColumn>
                    <DataGridTextColumn Header="Updates" Binding="{Binding UpdateStatus}" Width="Auto" MinWidth="80" IsReadOnly="True" ElementStyle="{StaticResource ResourceKey=StyleDataGridText}"/>
                </DataGrid.Columns>

对于第一列,即复选框。我想要一个列标题作为select all的复选框。如何在列标题中显示一个复选框。

1 个答案:

答案 0 :(得分:1)

您可以将HeaderTemplate设置为包含您想要的任何内容:

       <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate>
                        <CheckBox/>
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>