afree
(afree)
August 31, 2016, 1:21am
#1
Hi; i am trying to catch Cell value changed event on a WPF DataGrid
, the needed behavior is that we need an event to fire every time user clicks on a CheckBox DataGrid cell, However CellChangedEvent and CellEditingEvent are not doing the requested job at all.
i currently do this:
<DataGrid ItemsSource="{Binding MyItemsCollection}" CanUserAddRows="false" AutoGenerateColumns="true" CurrentCellChanged="DataGrid_CurrentCellChanged">
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<Style. Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="IsEditing" Value="true" />
</Trigger>
</Style. Triggers>
</Style>
</DataGrid.Resources>
</DataGrid>
So the question is, how can I trigger an event when the user Checks/Unchecks the Checkboxes inside the cells?
thanks
2 Likes
isoftech
(isoftech)
August 31, 2016, 1:55am
#2
I had the same issue, i solved it using something like this:
<EventSetter Event="CheckBox.Checked" Handler="InstallChecked" />
in the ElementStyle for the Columns i wanted, and i Implemented the InstallChecked
handler in my code behind in C#.
1 Like
sparta
(this.is.sparta)
August 31, 2016, 2:56am
#3
In your DataGrid add a handler for TargetUpdated event.
Set a column explicitly, and it would be nice if you set AutoGenerateColumns=False
.
In your Binding flag the NotifyOnTargetUpdated property (your target is your checkbox).
In your Binding UpdateSourceTrigger=PropertyChanged
and Mode=TwoWay
(not the default behavior of the DataGrid).
XAML :
<DataGrid AutoGenerateColumns="False" TargetUpdated="DataGrid_TargetUpdated" ItemsSource="{Binding MyItemsSource}">
<DataGrid.Columns>
<DataGridCheckBoxColumn Width="200" Binding="{Binding NotifyOnTargetUpdated=True, Path=SomeBooleanProperty, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
Event handler code behind:
private void DataGrid_TargetUpdated(object sender, DataTransferEventArgs e)
{
// handle it here!
}
2 Likes
afree
(afree)
September 1, 2016, 6:56am
#4
That’s exactly what I was looking for thanks for the tip @sparta