您可以将 collectionview 的源绑定到 observablecollecion。我的问题是:如果您使用的是 mvvm,为什么要以代码隐藏的方式执行所有这些操作?
ViewModel 构造函数:
public PrimarySearchViewModel()
{
this.SearchResultsCVS = new CollectionViewSource();
if (IsInDesignMode)
{
DesignMode_CreateSearchResults();
// Code runs in Blend --> create design time data.
}
else
{
//Messenger.Default.Register<IEnumerable<ReadmitPatientList>>(this, MessageTypes.EXECUTESEARCHREQUEST, RefreshSearchResults);
//Messenger.Default.Register<MessageTypes.EXECUTESEARCHREQUEST>>(this,ICollection<ReadmitPatientList>,RefreshSearchResults);
Messenger.Default.Register<Messages.DisplayReadmitPatientListMessage>(this, onReciveDisplayReadmitPatientListMessage);
Messenger.Default.Register<WavelengthIS.Core.Messaging.SaveNotification<QuestionairreViewModel>>(this, sn => ClearSearchResults());
// Code runs "for real": Connect to service, etc...
}
}
我通常使用设计时间服务来创建我的设计时间数据:但在这种情况下,我只是做了一个快速而肮脏的复制和粘贴:
private void DesignMode_CreateSearchResults()
{
this.SearchResults = new ObservableCollection<ReadmitPatientListViewModel>();
this.SearchResults.Add(new ReadmitPatientListViewModel(new ReadmitPatientList()
{
PatientID = 0000000,
PatientName = "Test Patient",
PatientDOB = Convert.ToDateTime("01/01/2010"),
OriginalAdmitDate = Convert.ToDateTime("01/01/2010 00:00:00"),
OriginalReason = "Becauselkahsdfkahsfkahsf",
OriginalVisitNumber = "0000000",
ReAdmitDate = Convert.ToDateTime("01/01/2010 00:00:00"),
ReAdmitReason = ";aasfkahsfashfa;lsfas",
ReAdmitVisitNumber = "9999999"
}
));
this.SearchResults.Add(new ReadmitPatientListViewModel(new ReadmitPatientList()
{
PatientID = 0000000,
PatientName = "Test Patient",
PatientDOB = Convert.ToDateTime("01/01/2010"),
OriginalAdmitDate = Convert.ToDateTime("01/01/2010 00:00:00"),
OriginalReason = "Becauselkahsdfkahsfkahsf",
OriginalVisitNumber = "0000000",
ReAdmitDate = Convert.ToDateTime("01/01/2010 00:00:00"),
ReAdmitReason = ";aasfkahsfashfa;lsfas",
ReAdmitVisitNumber = "9999999"
}
));
this.SearchResults.Add(new ReadmitPatientListViewModel(new ReadmitPatientList()
{
PatientID = 0000000,
PatientName = "Test Patient",
PatientDOB = Convert.ToDateTime("01/01/2010"),
OriginalAdmitDate = Convert.ToDateTime("01/01/2010 00:00:00"),
OriginalReason = "Becauselkahsdfkahsfkahsf",
OriginalVisitNumber = "0000000",
ReAdmitDate = Convert.ToDateTime("01/01/2010 00:00:00"),
ReAdmitReason = ";aasfkahsfashfa;lsfas",
ReAdmitVisitNumber = "9999999"
}
));
SearchResultsCVS_Refresh();
}
private void SearchResultsCVS_Refresh()
{
SearchResultsCVS.Source = this.SearchResults;
SearchResultsCVS.SortDescriptions.Clear();
SearchResultsCVS.SortDescriptions.Add(new System.ComponentModel.SortDescription("PatientLastName", System.ComponentModel.ListSortDirection.Ascending));
SearchResultsCVS.View.Refresh();
}
我使用 ViewModels 的 ObservableCollections。OC 的通知事件仅针对从集合中添加或删除的项目触发,通过使用实际列表项的 vm,如果需要,您可以获得项目属性的更改通知。
您还需要确保您的 ViewModelLocator 设置和定义正确。我现在发现了几篇关于人们使用 MVVMLight 但没有使用其中一些最强大功能的帖子。如果您按照设计使用它来使用它,它的工作原理就像它应该工作的那样......我可以证明这一点。
<!--Global View Model Locator-->
<local:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
我相信 IsDataSource 属性会告诉 Blend 将其放在 DataTab 上......但我并没有为我的 Datamanipulations 使用那么多混合,所以我并不担心它。