如何在网格 header 中获得 check-box

How to get a check-box in grid header

对于此应用程序,网格中的第一列是复选框类型,其 header 表示 Select。我希望 header 也显示一个复选框。 Checking/Unchecking 该复选框应选中或取消选中网格中的所有项目。我该怎么做?

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid Loaded="Data_Loaded" >
        <Grid.RowDefinitions>
            <RowDefinition Height="6*" />
            <RowDefinition  />
        </Grid.RowDefinitions>
        <DataGrid x:Name="grEmployees" HorizontalAlignment="Left" Margin="10,10,0,0"  CanUserAddRows="False" CanUserDeleteRows="False"
                  VerticalAlignment="Top" AlternatingRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False" Grid.Row="0">
            <DataGrid.Columns>
                <DataGridTemplateColumn  Header="Select" Width="2*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox x:Name="chkSelectedDevice" IsChecked="{Binding Path=Configure, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center"  />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName, Mode=OneWay}" Width="3*" />
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName, Mode=OneWay}"  Width="2*" />
                <DataGridTextColumn Header="Description" Binding="{Binding Description, Mode=OneWay}"  Width="5*" />
            </DataGrid.Columns>

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="5" Grid.Row="1" >
            <Button x:Name="btnClose" Content="Close" Margin="5" Width="50"  />
        </StackPanel>

    </Grid>

</Window>

public partial class MainWindow : Window
    {
        private List<Employee> Employees = null;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Data_Loaded(object sender, RoutedEventArgs e)
        {
            Employees = new List<Employee>()
            {
                new Employee() { IsHardWorking = false, LastName = "Silly", FirstName = "Dude", Description= "this due is a mess" },
                new Employee() { IsHardWorking = true, LastName = "Mean", FirstName = "Person", Description= "funny" },
                new Employee() { IsHardWorking = false, LastName = "New", FirstName = "Friend", Description= "let her go in next round of layoffs" },
                new Employee() { IsHardWorking = true, LastName = "My", FirstName = "Buddy", Description= "simply no comments" },
            };

            this.grEmployees.ItemsSource = Employees;
        }
    }
<Grid Loaded="Data_Loaded" >
    <Grid.RowDefinitions>
        <RowDefinition Height="6*" />
        <RowDefinition  />
    </Grid.RowDefinitions>
    <DataGrid x:Name="grEmployees" HorizontalAlignment="Left" Margin="10,10,0,0"  CanUserAddRows="False" CanUserDeleteRows="False"
              VerticalAlignment="Top" AlternatingRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False" Grid.Row="0">
        <DataGrid.Columns>
            <DataGridTextColumn>
                <DataGridTextColumn.Header>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox></CheckBox>
                        <TextBlock>Test</TextBlock>
                    </StackPanel>
                </DataGridTextColumn.Header>
            </DataGridTextColumn>
            <DataGridTemplateColumn  Header="Select" Width="2*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox x:Name="chkSelectedDevice" IsChecked="{Binding Path=Configure, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center"  />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="Last Name" Binding="{Binding LastName, Mode=OneWay}" Width="3*" />
            <DataGridTextColumn Header="First Name" Binding="{Binding FirstName, Mode=OneWay}"  Width="2*" />
            <DataGridTextColumn Header="Description" Binding="{Binding Description, Mode=OneWay}"  Width="5*" />
        </DataGrid.Columns>

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="5" Grid.Row="1" >
            <Button x:Name="btnClose" Content="Close" Margin="5" Width="50"  />
        </StackPanel>
    </DataGrid>
</Grid>

是的,幸运的是,有漂亮的内置方法可以自定义各种东西。在这种情况下,我们只需用我们自己的模板覆盖默认列 header 并在其中放置一个 CheckBox

<DataGridTemplateColumn.Header> 
   <CheckBox Name="ACheckBox"
             Checked="Do_Something" 
             Unchecked="Do_Something_Else"/>
</DataGridTemplateColumn.Header>

希望这对您有所帮助。干杯