I am looking to fire an event when a cell in a WPF DataGrid is clicked, I have tried
XAML
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<EventSetter Event="GotFocus" Handler="b1SetColor"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
C#
void b1SetColor(object sender, RoutedEventArgs e)
{
MessageBox.Show("Focused");
}
But nothing happens (doesn't fire) when I do click the Combobox cell. is there a way I can achieve this?
Use DataGridCellStyle
and hook PreviewMouseDown
event.
<DataGrid>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewMouseDown" Handler="b1SetColor"/>
</Style>
</DataGrid.CellStyle>
</DataGrid>
On the level of DataGrid
you can subscribe to SelectedCellsChanged
event:
XAML:
<DataGrid SelectedCellsChanged="selectedCellsChanged"/>
C#:
void selectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
MessageBox.Show("Clicked");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With