I\'m struggling to catch an event with a DataGrid. What I want to achieve is that when the user clicks ONCE on a checkbox of a datagrid cell, an event fires and I can get th
Try this simple way
private void myDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.Column.SortMemberPath.Equals("EndDate"))
{
if (((MyObjectInRow)e.Row.Item).DataFine.Equals(EndDate.MinValue))
{
((MyObjectInRow)e.Row.Item).Completed = 1;
}
else
{
((MyObjectInRow)e.Row.Item).Completed = 0;
}
}
}
1) In your DataGrid register for TargetUpdated event .
2) Specify a Column , and ideally set AutoGenerateColumns=False .
3) In your Binding flag the NotifyOnTargetUpdated property (your target is your checkbox).
4) In your Binding UpdateSourceTrigger=PropertyChanged and Mode=TwoWay (not the default behavior of the DataGrid).
XAML :
<DataGrid TargetUpdated="DataGrid_TargetUpdated"
AutoGenerateColumns="False"
ItemsSource="{Binding SomeValues, Mode=OneWay}" CanUserAddRows="False" >
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding Path=., NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
in CS: (where you might wan't to handle that event.)
private void DataGrid_TargetUpdated(object sender, DataTransferEventArgs e)
{
// Do what ever...
}
This is how I solved it. It's not the best of the solution but it works for me. As stated by @eranotzap I set set AutoGenerateColumns=False and UpdateSourceTrigger = PropertyChanged. Then I did the following:
<DataGrid Grid.ColumnSpan="2" Grid.Row="1" Grid.Column="0"
AutoGenerateColumns="False"
ItemsSource="{Binding MasterDataTable, Mode=TwoWay}"
CanUserAddRows="False"
Margin="10 5">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Fornitore}" Header="Fornitore" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=Stat}" Header="Stat" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=Intestazione}" Header="Intestazione" Width="*" IsReadOnly="True" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="AcceptsReturn" Value="true" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=PrzVend}" Header="PrzVend" Width="Auto" IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=DatEXPO}" Header="DatEXPO" Width="Auto" IsReadOnly="True" />
<DataGridCheckBoxColumn Binding="{Binding Path=Sel, NotifyOnSourceUpdated=False, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Header="Sel" Width="Auto">
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="CheckBox.Checked" Handler="CellChanged"/>
<EventSetter Event="CheckBox.Unchecked" Handler="CellChanged"/>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsReadOnly" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="IsEditing" Value="True" />
</MultiTrigger>
</Style.Triggers>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
<DataGridTextColumn Binding="{Binding Path=CodComp}" Header="CodComp" Width="Auto" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
In the code behind the method CellChanges is called every time the checkbox is checked or unchecked. To get the value I do the following:
void CellChanged(object sender, RoutedEventArgs e)
{
if (sender as DataGridCell != null && (sender as DataGridCell).Column != null && (sender as DataGridCell).Column.Header != null)
{
bool? isSelected = (e.OriginalSource as ToggleButton).IsChecked;
}
}
Hope it helps somebody.