Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current row when edit button clicked in WPF DataGrid?

Tags:

c#

wpf

datagrid

in my DataGrid the last column is a TemplateColumn that has an Edit Button, the current implementation is that the user needs to check a checkbox beside the row and then click the edit button, obviously, this is nonsense, so isn't there a way to get the current row when I click the related edit button?

like image 705
mshwf Avatar asked Feb 17 '26 11:02

mshwf


1 Answers

The "current row" is the DataContext of the Button so you could cast the sender argument and the DataContext property:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Button button = sender as Button:
    YourClass selectedRow = button.DataContext as YourClass;
    //...
}
like image 70
mm8 Avatar answered Feb 20 '26 00:02

mm8