我有一个概念问题,我有这样的情况: 我有MainWindow,在这个窗口上有一个按钮,可以将子视图从一个切换到另一个。现在在子视图1上我从数据库中获取一些信息,我使用类似的构造函数将该信息传递给子视图2:
public DataParsingViewModel(List<ProcessedEventsData> preprocessedEvents)
{
Service = new EventService();
if (this.preprocessedEvents == null || (preprocessedEvents.Count != this.preprocessedEvents.Count))
{
this.preprocessedEvents = preprocessedEvents;
PopulateAxmFilter();
PopulateSrcChannelFilter();
PopulateTimestampsFilter();
PopulateEventCodeFilter();
}
foreach (ProcessedEventsData preprocessedEvent in preprocessedEvents)
{
PreprocessedEvents.Add(preprocessedEvent);
}
}
从主窗口视图模型调用:
private void SetDataParsingView()
{
List<ProcessedEventsData> eventList = preprocessingViewModel.Events.ToList();
ContentControlDataContext = eventList.Any() ? new DataParsingViewModel(preprocessingViewModel.Events.ToList()) : new DataParsingViewModel();
}
现在我的情况是,如果按子视图编号2,每次属性重新加载。是否可以这样做,只有在第一个子视图中preprocessedEvents发生变化时才重新加载?
答案 0 :(得分:1)
不确定。只需将eventList
保存在属性或字段中而不是本地变量中,只有在数据库中尚不存在时才将其填充。
private List<ProcessedEventsData> eventList;
private void SetDataParsingView()
{
if(this.eventList == null)
{
this.eventList = preprocessingViewModel.Events.ToList();
}
ContentControlDataContext = eventList.Any() ? new DataParsingViewModel(preprocessingViewModel.Events.ToList()) : new DataParsingViewModel();
}