WPF:如何在 DataGridCheckBoxColumn 中添加 Checked 和 Unchecked 事件?
WPF : How to add Checked and Unchecked events in the DataGridCheckBoxColumn?
WPF:如何在 DataGridCheckBoxColumn 中添加 Checked 和 Unchecked 事件?
<DataGridCheckBoxColumn Header="Choose" x:Name="choose">
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
<EventSetter Event="CheckBox.Checked" Handler="OnChecked"/>
<EventSetter Event="CheckBox.Unchecked" Handler="OnChecked"/
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
您的 XAML 工作正常,问题在于您是否获得了复选框。您应该能够从使用未选中或已选中事件触发的复选框访问元素。
示例:
var ch = sender as Checkbox;
var row = data_kala.ItemContainerGenerator.ContainerFromItem(ch) as DataGridRow;
bool ischecked = ch.IsChecked;
if (ischecked) {
row.BackGround = Brushes.Gray;
}
else {
row.BackGround = Brushes.White;
}
WPF:如何在 DataGridCheckBoxColumn 中添加 Checked 和 Unchecked 事件?
<DataGridCheckBoxColumn Header="Choose" x:Name="choose">
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
<EventSetter Event="CheckBox.Checked" Handler="OnChecked"/>
<EventSetter Event="CheckBox.Unchecked" Handler="OnChecked"/
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
您的 XAML 工作正常,问题在于您是否获得了复选框。您应该能够从使用未选中或已选中事件触发的复选框访问元素。
示例:
var ch = sender as Checkbox;
var row = data_kala.ItemContainerGenerator.ContainerFromItem(ch) as DataGridRow;
bool ischecked = ch.IsChecked;
if (ischecked) {
row.BackGround = Brushes.Gray;
}
else {
row.BackGround = Brushes.White;
}