I found this: Close Window from ViewModel which gets me started down the path of modifying my DelegateCommand class to handle parameters. But I am not able to get the syntax worked out.
Here is my DelegateCommand class, and the DelegateCommand class that I'm trying to create with little success:
public class DelegateCommand : ICommand
{
private readonly Action _action;
public DelegateCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return true;
}
#pragma warning disable 67
public event EventHandler CanExecuteChanged { add { } remove { } }
#pragma warning restore 67
}
public class DelegateCommand<T> : ICommand
{
private readonly Action _action;
public DelegateCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return true;
}
#pragma warning disable 67
public event EventHandler CanExecuteChanged { add { } remove { } }
#pragma warning restore 67
}
And here is what I do in the viewmodel:
public ICommand RowEditEndingAction
{
get { return new DelegateCommand(RowEditEnding); }
}
public ICommand UpdateDatabaseClick
{
get { return new DelegateCommand<object>(UpdateDatabase); } //THIS LINE HAS THE ERROR
}
And the actual method that will get called:
public void UpdateDatabase(object parameter)
{
Window w = (Window)parameter;
// A bunch of stuff that works is removed for brevity
w.Close();
}
The compiler does not like my UpdateDatabaseClick, specifically saying there is something wrong with the arguments to DelegateCommand, but I am at a loss as to what I am doing wrong (though I am thinking it is syntax . . . ). What do I need to change? This all worked before I added the parameter to UpdateDatabase, and just had the DelegateCommand (no template class). But in that case I could not close the window.