TreeView 未填充来自 Entity Framework 的 HierarchicalDataTemplate 数据

TreeView not populating HierarchicalDataTemplate data from Entity Framework

几个小时以来,我一直在查看 HierarchialDataTemplate 上的许多帖子,但似乎无法确定我哪里出错了。我有一个模型 Person,我使用 Entity Framework 6.

检索它
public partial class Person
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Person()
    {
        this.Complaints = new HashSet<Complaint>();
        this.PeopleXAddresses = new HashSet<PeopleXAddress>();
    }

    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int StreetID { get; set; }
    public string Area { get; set; }
    public string Phone { get; set; }
    public string Ext { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Complaint> Complaints { get; set; }
    public virtual Person People1 { get; set; }
    public virtual Person Person1 { get; set; }
    public virtual Street Street { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<PeopleXAddress> PeopleXAddresses { get; set; }
}

PeopleXAddress:

public partial class PeopleXAddress
{
    public int PersonId { get; set; }
    public int Id { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }

    public virtual Person Person { get; set; }
}

我用调试器检查过PeopleXAddress

中有记录

XMAL:

<TreeView ItemsSource="{Binding PeopleList}"
          Grid.Column="0"
          Grid.Row="1"
          Grid.RowSpan="8"
          >
    <TreeView.Resources>
        <HierarchicalDataTemplate ItemsSource="{Binding PeopleXAddresses}"
                                  DataType="{x:Type this:Person}">
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding LastName}"
                       Foreground="Black" />
                <Label Content="{Binding FirstName}"
                       Foreground="Black" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <DataTemplate DataType="{x:Type this:PeopleXAddress}">
            <Label Content="{Binding Path=Address}"
                   Foreground="Black" />
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

我看到 FirstNameLastName 但没有 Address

获取数据

  public List<Person> People()
    {
        using (var context = new SewerMaintenanceEntities())
        {
            return context.People
                .Select(p=> p)
                .ToList();
        }
    }

更新
当我检查 Person 的 PeopleXAddress 时,他们现在出现了。但是只有我查的人,其余的人没有。

探索了一天后,我发现问题出在我获取数据的位置。

 public List<Person> People()
    {
        using (var context = new SewerMaintenanceEntities())
        {
            return context.People
                .Select(p=> p)
                .Include(p=>p.PeopleXAddresses)
                .ToList();
        }
    }