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?

5
  • 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 Commented Mar 19, 2019 at 22:19
  • Why do you need the row (or row index)? Commented Mar 19, 2019 at 23:23
  • To modify some data in that row (not user related) Commented Mar 20, 2019 at 0:16
  • Clearly, the DataContext of the rows is not of type MyObject. What is the type of the items of the list? Commented Mar 20, 2019 at 0: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? Commented Mar 20, 2019 at 13:56

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.