3

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 !

Pierre
  • 490
  • 1
  • 7
  • 26
  • Could you please include the complete xaml for ` – sujith karivelil Aug 10 '16 at 08:04
  • I edited my post by adding the entire `DataGrid`. Indeed, I didn't use `CommandParameter`, I didn't know this property – Pierre Aug 10 '16 at 08:09
  • You need to add some command parameters: [try this example](http://stackoverflow.com/questions/12371253/how-do-i-pass-a-variable-as-a-commandparameter) – sujith karivelil Aug 10 '16 at 08:15
  • I don't know if I was clear, but I don't want to put as parameter a property from a class, I just would like to get the index of the row where the button is – Pierre Aug 10 '16 at 08:22

2 Answers2

6

For those who have the same problem than me, this is the solution :

In your XAML, you have to add the parameter to your button. It contains the index of the row ! (don't change anything, it's working independently to the name of the DataGrid)

CommandParameter="{Binding Path=SelectedIndex, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}">Delete</Button>

Then, in your ViewModel, you have to create your RelayCommand

public RelayCommand<Object> DeleteRowCriterionCommand { get; private set; }

You have to add the <Object> if you want to take the parameter that you added in the CommandParameter. Then, you instance your property in your constructor

DeleteRowCriterionCommand = new RelayCommand<Object>(param => DeleteRowCriterion(param), param => CanDeleteRowCriterion());

Now, you can use your parameter in your function :

private void DeleteRowCriterion(object parameter)
{
    UserCriteria.RemoveAt(Convert.ToInt32(parameter));
}

I hope that it will hope some of you :)

Pierre
  • 490
  • 1
  • 7
  • 26
0

How about using the button as "sender" and then getting the SelectedIndex of the the Datagrid by parent?

hYg-Cain
  • 348
  • 3
  • 10
  • How do you do that ? Because this is not using binding, isn't it ? – Pierre Aug 10 '16 at 09:06
  • public void DeleteRowCriterion(object sender, RoutedEventArgs e) { var button = (Button) sender; Datagrid dg = button.GetParent() //forgot the acutal method but maybe this helps as basic } – hYg-Cain Aug 10 '16 at 09:21
  • It works like you are doing, but this is not using binding. I found how to do it, but thanks for your time ! – Pierre Aug 10 '16 at 12:13
  • Oh sorry, glad you found what you were looking for. Nice solution thanks for sharing – hYg-Cain Aug 10 '16 at 12:48