View is responsible for this. You can simply use codebehind to control the visibility of user interface elements in response to user actions in the user interface.
Sometimes it's better to be practical than to be harshly dogmatic.
So now that you have edited your question, this becomes a completely different question.
Your DataGrid should be bound to a set of elements.
Your button must be bound to a command in the ViewModel, and CommandParameter must be the Model to which a specific row is bound.
<DataTemplate> <Button Content="Remove" Command="{Binding DataContext.RemoveItemCommand, ElementName=theWindow}" CommandParameter="{Binding}" /> </DataTemplate>
Pay attention to some important things here. We need to bind to the ICommand in the ViewModel as part of the template. ViewModel is a DataContext of a window. In this example, the window is called "theWindow" ( x:Name="theWindow" ). Since the binding source is a window, Path must point to the ViewModel in the DataContext property in this window.
We pass the current model to which the DataGrid row is bound to the command. Thus, triival removes it from the collection in the ViewModel.
public ObservableCollection<Model> Items {get;set;} public ICommand RemoveItemCommand {get;set;}
It is assumed that you are using one of the standard delegated ICommand implementations. You can see how trivial to implement this, and since the collection is observable, after clicking the button and deleting the model, the DataGrid will be notified of the collection change and delete this row.
source share