I want to recover this thread. I have the same situation, but any of the solutions is working for me. I have the following xml, as you can se similar to the one ask:
<DataGrid Grid.Column="1" Name="Info_DG" FontSize="18" CellEditEnding="Info_DG_CellEditEnding" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding BM}"/>
<DataGridTextColumn Binding="{Binding BC}"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="Button_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
My objective is the same as the thread: to get the row number of the pressed button, but any of the listed solutions works for me.
Both of the following solutions are not working, both DataContext and ID are null in my case:
MyObject obj = ((FrameworkElement)sender).DataContext as MyObject;
object ID = ((Button)sender).CommandParameter;
The other answers are also base in DataContext, thus is not working as it is null. Is there anything wrong in my code as it gives me a null datacontext?
DataContextof yourButtonshould be the item of your collection for that row. Try using the live tree explorer from vs to check what theDataContextisItemsSourceproperty ofInfo_DGand to what? And what value are you tryin g to get in the event handler?