0

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?

  • The `DataContext` of your `Button` should be the item of your collection for that row. Try using the live tree explorer from vs to check what the `DataContext` is – Ackdari Mar 19 '19 at 22:19
  • Why do you need the row (or row index)? – Shawn Kendrot Mar 19 '19 at 23:23
  • To modify some data in that row (not user related) –  Mar 20 '19 at 00:16
  • Clearly, the DataContext of the rows is not of type MyObject. What is the type of the items of the list? – E-Bat Mar 20 '19 at 00:18
  • @UncoProg: How do you set the `ItemsSource` property of `Info_DG` and to what? And what value are you tryin g to get in the event handler? – mm8 Mar 20 '19 at 13:56

1 Answers1

0

I think you are missing the ItemSource?

<DataGrid ItemSource="{Binding YourItemSource}" 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>

With your Data Context issue, it is better to understand what is Data Context first, please read the below link. The below post explains in the easiest way of how to assign your Data Context which is step 1. However, you should always link your Data Context to a View Model (which is step 2).

https://www.wpf-tutorial.com/data-binding/using-the-datacontext/

Anson Fong
  • 550
  • 3
  • 9