I'm having trouble figuring out what the best solution is given the following situation. I'm using Prism 4.1, MEF, and .Net 4.0.
I have an object Project
that could have a large number (~1000) of Line
objects. I'm deciding whether it is better to expose an ObservableCollection<LineViewModel>
from my ProjectViewModel
and manually create the Line viewmodels there OR set the ListBox as it's own region and activate views that way.
I'd still want my LineViewModel
to have Prism's shared services (IEventAggregator, etc.) injected, but I don't know how to do that when I manually create the LineViewModel
. Any suggestions or thoughts?
EDIT: My initial thoughts:
Project:
public class Project
{
public List<Line> Lines { get; set; }
}
ProjectViewModel:
[Export(typeof(ProjectViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ProjectViewModel : NotificationObject, IRegionMemberLifetime
{
private Project _localProject;
/*
HERE WILL BE SOME PROPERTIES LIKE COST, PRICE THAT ARE CUMULATIVE FROM THE Lines
*/
public ObservableCollection<LineViewModel> Lines { get; private set; }
private readonly IEventAggregator _eventAggregator;
[ImportingConstructor]
public ProjectViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<ProjectLoaded>().Subscribe(SetupProject, false);
Lines = new ObservableCollection<LineViewModel>();
}
private void SetupProject(Project project)
{
_localProject = project;
foreach(var l in _localProject.Lines)
{
LineViewModel lvm = new LineViewModel(l);
lvm.PropertyChanged += // Some handler here
Lines.Add(lvm);
}
}
public bool KeepAlive
{
get { return false; }
}
}
LineViewModel:
public class LineViewModel : NotificationObject
{
private Line _localLine;
public decimal Cost
{
get { return _localLine.Cost; }
set
{
_localLine.Cost = value;
RaisePropertyChanged(() => Cost);
}
}
public LineViewModel(Line incoming)
{
_localLine = incoming;
}
}