Reputation: 1899
Here is my Employee class.
public class Employee
{
public string LastName { get; set; }
public string FirstName { get; set; }
public bool Fire { get; set; }
}
This is how XAML is setup.
<Window x:Class="WpfApplication1.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="EmployeesGridLoaded">
<DataGrid x:Name="gEmployees" HorizontalAlignment="Left" Margin="10,10,0,0"
VerticalAlignment="Top" AlternatingRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False" Grid.Row="0">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Fire" Binding="{Binding Fire}" Width="1*" />
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" Width="3*" />
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" Width="3*" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
And finally here is the EmployeesGridLoaded method.
private void EmployeesGridLoaded(object sender, RoutedEventArgs e)
{
List<Employee> Employees = new List<Employee>()
{
new Employee() { Fire = false, LastName = "Silly", FirstName = "Dude" },
new Employee() { Fire = false, LastName = "Mean", FirstName = "Person" },
};
gEmployees.ItemsSource = Employees;
}
}
Problem is when I click on the Fire check-box first time, it does not change it state to checked right away. I have to click one more time to get its state changed to check. It probably is selecting row the first time. Is there anyway of getting it checked first time when I clicked on this cell and row wasn't already selected in the grid?
Upvotes: 2
Views: 1228
Reputation: 241
Replace this line of code :
<DataGridCheckBoxColumn Header="Fire" Binding="{Binding Fire}" Width="1*" />
With:
<DataGridTemplateColumn Header="Fire" Width="1*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Fire, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Upvotes: 4