i have a List that i would like to pass to the ViewModel.Intuitively i thought it would be straightforward by passing it is a parameter to the ViewModel's constructor but on doing that i get the error that a ViewModel's constructor should be parameterless...This being the case:
public SomeMethod(){
List<T> list = new List<T>();
ViewModel vm = new ViewModel(list);
}
the viewmodel:
public class ViewModel
{
public ObservableCollection<T> Collection { get; set; }
public ViewModel(List<T> t)
{
Collection = new ObservableCollection<T>();
foreach (T item in t)
{
this.Collection.Add(new T (item.value));
}
}
what other ways can i pass the list to the ViewModel..for one i thought of creating another method in the ViewModel that accepts the list as a parameter,but how would i be able to call it from the constructor since the constructor has to pass the parameter to it too.
Just leave the Constructor empty and modify your Collection
-Property to Property with Backing field.
After this, you can do in your setter whatever you want.
private ObservableCollection<T> _collection
public ObservableCollection<T> Collection {
get{
return this._collection;
}
set{
this._collection = value;
// Do what you want here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.