I have a DataGrid which contains on each row a delete button like this :
<Button Command="{Binding DeleteRowCriterionCommand}">Delete</Button>
But I don't know how to get the currentItem property without using the DataGrid.Name
<!-- 2ND : CRITERIA -->
<Grid>
<DataGrid ItemsSource="{Binding UserCriteria, Mode=TwoWay}" SelectedItem="{Binding SelectedItemDG, Mode=TwoWay}" AutoGenerateColumns="False">
<DataGrid.Columns>
<!--TEXTBOX FOR SQL VALUES-->
<DataGridTemplateColumn Header="SQLValue" Width="600">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- BUTTON FOR DELETING -->
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding DeleteRowCriterionCommand}">Delete</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
I'm using MVVMLight, so I know how to connect my command to my ViewModel, but the only thing missing is to get the index of the row where I click the button.
This is the function by using DataGrid.Name :
public void DeleteRowCriterion()
{
// I would like to replace the first line by not using DataGridName
// int currentRowIndex = DataGridName.Items.IndexOf(CriteriaDG.CurrentItem);
// UserCriteria.RemoveAt(currentRowIndex);
}
If someone knows how to do it, I'll appreciate that !