Reputation: 1899
With this application, first column in grid is check box type with its header says Select. I would like the header to show a check box also. Checking/Unchecking that check box should check or uncheck all items in the grid. How can I do that?
<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;
}
}
Upvotes: 1
Views: 822
Reputation: 23280
Right, so luckily there's nifty built in ways to customize all sorts of things. In this case we'll just override the default column header template with our own and plop a CheckBox
in there.
<DataGridTemplateColumn.Header>
<CheckBox Name="ACheckBox"
Checked="Do_Something"
Unchecked="Do_Something_Else"/>
</DataGridTemplateColumn.Header>
Hope this helps. Cheers
Upvotes: 1
Reputation: 1814
<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>
Upvotes: 1