未选择行时,复选框列不会在第一次更改其状态
本文关键字:第一次 状态 选择 复选框 | 更新日期: 2023-09-27 18:34:34
这是我的员工类。
public class Employee
{
public string LastName { get; set; }
public string FirstName { get; set; }
public bool Fire { get; set; }
}
这就是 XAML 的设置方式。
<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>
最后是EmployeesGridLoaded方法。
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;
}
}
问题是当我第一次单击Fire复选框时,它不会立即将其状态更改为选中状态。我必须再单击一次才能更改其状态以进行检查。它可能是第一次选择行。当我单击此单元格并且尚未在网格中选择行时,是否可以第一次检查它?
替换这行代码:
<DataGridCheckBoxColumn Header="Fire" Binding="{Binding Fire}" Width="1*" />
跟:
<DataGridTemplateColumn Header="Fire" Width="1*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Fire, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>