【发布时间】:2020-05-17 18:45:58
【问题描述】:
我有一个包含两个部分的页面: 带有 itemsource 的 Listview 与一个类绑定,ObservableCollection “Patients”加载了“Patient”类。 在 Listview 下是与选定项 Patient 绑定的文本框。除了在 selection_changed 中滚动到所选项目之外,一切都无需在页面中编写任何代码即可工作。
第二个列表视图必须显示来自所选“患者”的详细信息“访问”。
应用程序在 MVVM 框架中工作,其 Viewmodel 包含页面的属性。
问题是使两个ListView之间的关系。我首先尝试在 NotifyPropertyChanged 事件中构建第二个列表“访问”:
if (Patient.ID > 0)
{
LoadVisite(Patient.ID); // fill the details list "Visites"
NotifyPropertyChanged("Visites");
}
选择“患者”时不显示延迟。
我尝试了另一种解决方案,将详细信息列表插入到大师班“像这样的病人:
public Class Patient
...
public ObservableCollection<ClsVisite> Visites
{
get
{
return _visites;
}
set
{
_visites = value;
}
}
// WDABase class to open the database and load data connection
WDABase wd = new WDABase();
wd.LoadListeVisites(ID, _visites); //ID is the relation beween the two tables
}
}
现在我尝试像这样在 XAML 中创建 Listview 详细项目源:
<ListView Name="ListeVisites" ItemsSource="{Binding Path=Patient.Visites}" SelectedItem="{Binding Visite}">
没有显示细节。
我发现的唯一解决方案是像这样在主 Listview 的 selection_changed 事件中添加一些代码(在这种情况下,listviews 在两个不同的框架中):
private void ListePatients_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ListePatients.ScrollIntoView(ListePatients.SelectedItem);
if(ListePatients.SelectedItem != null)
{
var w1 = ((System.Windows.FrameworkElement)App.Current.MainWindow.Content).Parent;
Frame pageVisite = (w1 as MainWindow).Visit;
var w2 = (System.Windows.FrameworkElement)pageVisite.Content;
ListView Lv = (w2 as Visite).ListeVisites;
Lv.ItemsSource = (ListePatients.SelectedItem as ClsPatient).Visites;
}
}
它的工作原理,但是否有另一种优雅的解决方案来绑定细节 Listview 的 itemsource ? 谢谢你帮助我。 让-玛丽
【问题讨论】:
标签: wpf listview binding master-detail