I've been using Rachel's solution to bind a button with a Command : https://stackoverflow.com/a/3531935/4713963
Now I would like to do the same within a DataGrid.
Sample code :
<DataGrid.Columns>
<DataGridTemplateColumn Header="CustomerID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding CustomerId}"
Command="{Binding DataContext.DetailCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
with
private ICommand _detailCommand;
public ICommand DetailCommand
{
get
{
if (_detailCommand == null)
{
_detailCommand = new RelayCommand(
param => this.Execute(),
);
}
return _detailCommand;
}
}
private void Execute()
{
MessageBox.Show("Selected CustomerId : ");
}
Doing so I can invoke a Command, now my question is how to pass the CustomerId property as a parameter to the Execute() method ?