I have a lack of understanding how to use the ICommand inferface for executing multiple methods in a row. Ideally I would like to create a delegate that executes all of the following 4 methods by invoking it (but it doesn't have to be a delegate):
var mongo = DbConn(); //Connection to MongoDB
var artikel = GetArtikel(addReorder reorderView); //returns ObservableCollection<ArtikelModel>
var reorder = GetReorder(artikel, reorderView); //returns ReorderModel
Insert(mongo, artikel, reorder); //inserts new reorder into the MongoDB database (one reorder has multiple articles(artikel))
The View (excerpt):
<Button Name="btnSave" Margin="3,0,3,0" Content="Save Reorder"
BorderThickness="0" BorderBrush="Green"
Height="30" FontWeight="Bold" FontSize="15"
Background="Green"
VerticalAlignment="Top"
Command="{Binding SaveNb}">
</Button>
Code Behind:
public addReorderView()
{
this.DataContext = new addReorderViewModel();
}
The ViewModel (need help here):
public class addReorderViewModel
{
//Hook to class DelegateCommand
private readonly DelegateCommand<Button> _clickCommand;
private ICommand saveNb { get; set; }
public addReorderViewModel()
{
}
public ICommand SaveNb
{
get
{
return saveNb;
}
set
{
saveNb = value;
}
}
The ViewModel has a "hook" to the DelegateCommand.cs-Class: DelegateCommand.cs
public class DelegateCommand<T> : System.Windows.Input.ICommand
{
private readonly Predicate<T> _canExecute;
private readonly Action<T> _execute;
public DelegateCommand(Action<T> execute)
: this(execute, null)
{
}
public DelegateCommand(Action<T> execute, Predicate<T> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
return true;
return _canExecute((parameter == null) ? default(T) : (T)Convert.ChangeType(parameter, typeof(T)));
}
public void Execute(object parameter)
{
_execute((parameter == null) ? default(T) : (T)Convert.ChangeType(parameter, typeof(T)));
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}
Questions: 1. What do I have to do to make the buttonclick execute the above mentioned 4 methods? 2. How do I add other buttonclick events (the view has about 5-10 buttons) into the ViewModel?
Aucun commentaire:
Enregistrer un commentaire