WPF - 冻结WPF嵌套ListView

时间:2018-01-19 08:56:11

标签: wpf xaml listview datatemplate

我有一个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>

enter image description here

在这里,我想在用户向下滚动时冻结列表顶部的医生姓名。如果向下滚动,医生名称将被隐藏。我的目标是如果用户向下滚动,患者姓名只需要滚动。医生的名字必须冻结在顶部。这样用户可以与相应的医生一起查看完整的患者列表而不会产生混淆。请帮忙。

1 个答案:

答案 0 :(得分:0)

以下示例显示了如何将结构转换为ListView GridView,其中每列都是医生。

假设源数据结构如下:

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,如下所示:

<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)),
            });
        }
    }
}

这假设一组静态数据,如果数据在初始加载后动态变化,则需要添加更多逻辑。