TreeView没有从Entity Framework填充HierarchicalDataTemplate数据

时间:2016-02-10 21:06:02

标签: c# entity-framework treeview

我已经在HierarchialDataTemplate上看了好几个小时的帖子已经好几个小时了,似乎无法将手指缠绕在我出错的地方。我有一个使用Entity Framework 6检索的模型 Person

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

更新
当我检查PeopleXAddress的Person时,它们现在显示出来。但只有我检查其余部分的人不会。

1 个答案:

答案 0 :(得分:0)

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

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