我有一个包含文本块和作为数据模板的ListView的ListView。下面是我的代码。
<ListView x:Name="DoctorsList" Grid.Column="1" VerticalContentAlignment="Top">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding DoctorName}" HorizontalAlignment="Center"/>
<ListView ItemsSource="{Binding AppointmentDetails}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="2 2 0 0">
<TextBlock Text="{Binding PatientName}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在这里,当用户向下滚动时,我想冻结列表顶部的医生姓名。现在,如果向下滚动,医生姓名将被隐藏。我的目标是,如果用户向下滚动,那么只需要滚动患者的姓名。医生的名字必须在最上面定格。以便用户可以与相应的医生一起查看完整的患者列表,而不会产生混淆。请帮帮忙。
发布于 2018-01-19 18:19:01
下面的示例展示了如何将结构转换为具有GridView
的ListView
,其中每一列都是一个医生。
假设源数据结构如下:
public class DoctorsVM
{
public DoctorsVM()
{
Doctors = new List<Doctor>();
}
public ICollection<Doctor> Doctors { get; set; }
}
public class Doctor
{
public Doctor()
{
AppointmentDetails = new List<AppointmentDetail>();
}
public string DoctorName { get; set; }
public ICollection<AppointmentDetail> AppointmentDetails { get; set; }
}
public class AppointmentDetail
{
public string PatientName { get; set; }
}
您可以创建以下转换:
public class DoctorAppointmentCollectionVM
{
public DoctorAppointmentCollectionVM()
{
Doctors = new List<Doctor>();
Entries = new List<DoctorAppointmentLineItem>();
}
public List<Doctor> Doctors { get; set; }
public List<DoctorAppointmentLineItem> Entries { get; set; }
}
public class DoctorAppointmentLineItem
{
public DoctorAppointmentLineItem()
{
AppointmentLine = new List<AppointmentDetail>();
}
public List<AppointmentDetail> AppointmentLine { get; set; }
}
public DoctorAppointmentCollectionVM DoctorsToLineCollection(DoctorsVM docs)
{
var result = new DoctorAppointmentCollectionVM();
for (int i = 0; i < docs.Doctors.Count; i++)
{
var doc = docs.Doctors.ElementAt(i);
result.Doctors.Add(doc);
for (int j = 0; j < doc.AppointmentDetails.Count; j++)
{
var appointment = doc.AppointmentDetails.ElementAt(j);
DoctorAppointmentLineItem patientArray;
if (j >= result.Entries.Count)
{
patientArray = new DoctorAppointmentLineItem();
patientArray.AppointmentLine.AddRange(Enumerable.Range(0, docs.Doctors.Count).Select(x => (AppointmentDetail)null));
result.Entries.Add(patientArray);
}
else
{
patientArray = result.Entries[j];
}
patientArray.AppointmentLine[i] = appointment;
}
}
return result;
}
并创建一个列表视图,如下所示:
<ListView x:Name="lv1" ItemsSource="{Binding Entries}" Loaded="lv1_Loaded">
<ListView.View>
<GridView x:Name="gv1">
</GridView>
</ListView.View>
</ListView>
为每个医生创建一个列
private void lv1_Loaded(object sender, RoutedEventArgs e)
{
var data = lv1.DataContext as DoctorAppointmentCollectionVM;
if (data != null)
{
gv1.Columns.Clear();
for (int i = 0; i < data.Doctors.Count; ++i)
{
gv1.Columns.Add(new GridViewColumn
{
Header = data.Doctors[i].DoctorName,
DisplayMemberBinding = new Binding(string.Format("AppointmentLine[{0}].PatientName", i)),
});
}
}
}
这假设是一组静态数据,如果数据在初始加载后动态更改,则需要添加更多逻辑。
https://stackoverflow.com/questions/48337298
复制相似问题