如何在网格标题中获取复选框

时间:2015-01-08 22:24:04

标签: c# .net wpf xaml

使用此应用程序,网格中的第一列是复选框类型,其标题为“选择”。我希望标题也显示一个复选框。选中/取消选中该复选框应选中或取消选中网格中的所有项目。我怎么能这样做?

<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;
        }
    }

2 个答案:

答案 0 :(得分:1)

<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>

答案 1 :(得分:1)

是的,幸运的是,有很好的内置方式来定制各种各样的东西。在这种情况下,我们只需使用我们自己的默认列标题模板覆盖默认列标题模板,然后在其中填充CheckBox

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

希望这会有所帮助。干杯