На основании вашего комментария вы должны попробовать это тогда (DataGrid
называется dataGrid
в XAML):
private void Button1_Click(object sender, RoutedEventArgs e)
{
// If the grid is populated via a collection binding the SelectedItem will
// not be a DataGridRow, but an item from the collection. You need to cast
// as necessary. (Of course this can be null if nothing is selected)
var row = (DataGridRow)dataGrid.SelectedItem;
}
Можно использовать Tag
( Редактировать:Если вы используете CheckBoxColumn, вы можете использовать стили для этого, если у вас есть проблемы с этим, я мог бы привести пример ):
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="Button1_Click"
Tag="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
private void Button1_Click(object sender, RoutedEventArgs e)
{
var button = (FrameworkElement)sender;
var row = (DataGridRow)button.Tag;
//...
}