首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >WPF -冻结WPF嵌套ListView中的第一项

WPF -冻结WPF嵌套ListView中的第一项
EN

Stack Overflow用户
提问于 2018-01-19 16:56:11
回答 1查看 380关注 0票数 0

我有一个包含文本块和作为数据模板的ListView的ListView。下面是我的代码。

代码语言:javascript
复制
<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>

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

EN

回答 1

Stack Overflow用户

发布于 2018-01-19 18:19:01

下面的示例展示了如何将结构转换为具有GridViewListView,其中每一列都是一个医生。

假设源数据结构如下:

代码语言:javascript
复制
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; }
}

您可以创建以下转换:

代码语言:javascript
复制
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;
}

并创建一个列表视图,如下所示:

代码语言:javascript
复制
<ListView x:Name="lv1" ItemsSource="{Binding Entries}" Loaded="lv1_Loaded">
    <ListView.View>
        <GridView x:Name="gv1">
        </GridView>
    </ListView.View>
</ListView>

为每个医生创建一个列

代码语言:javascript
复制
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)),
            });
        }
    }
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48337298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档