I\'m currently converting a small WPF project to MVVM. I have a List
in the ViewModel of the main window that my ItemsControl binds to and uses
You can try to keep your Command in your Model.
public class MyModel
{
public MyModel()
{
MyCommand = new DelegateCommand(MyCommandExecute);
}
public ICommand MyCommandCommand { get; set; }
private void MyCommandExecute()
{
}
}
And then, you must have an ObservableList for the list of your Items in your ViewModel as,
public class MyViewModel
{
public MyViewModel()
{
MyStarterCommand = new DelegateCommand(MyCommandExecute);
if (!IsDesignTime)//(bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(DependencyObject)).Metadata.DefaultValue;
MyCommand.Execute(null);
}
private ObservableCollection list;
private ICommand MyStarterCommand { get; set; }
public ObservableCollection List
{
get { return list; }
set
{
list = value;
RaisePropertyChanged(() => List);
}
}
private void MyStarterCommandExecute()
{
List = new ObservableCollection();
//Fill the list here
List.Add(new MyModel());
}
}
Then in XAML you must say;