,c#,wpf,entity-framework,xaml,entity-framework-core"/>

首页 > 解决方案 > 将 GridViewColumn 绑定到列表中的空属性

问题描述

    public partial class Entity
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
            
        public virtual AnotherEntity AnotherEntity { get; set; }
        ...
    }
    ...
    WPFPage()
    {
        _entities = DBContext.Entities.ToList();
    }
<ListView ItemsSource="{Binding _entities}" ...> ...
    <GridView>                    
        <!--with FECore5 no data here. But in EF6 there is.-->
        <GridViewColumn DisplayMemberBinding="{Binding Entity.AnotherEntityName}" 
        Header="Entity"
        Width="100"></GridViewColumn>
    </GridView>
...

除非我从应用程序代码中的 AnotherEntities 属性访问并加载另一个数据库实体:

    public AnotherWPFPage() { _anotherEntities = DBContext.AnotherEntities.ToList(); }

或者,如果我明确将绑定设置为

    <GridViewColumn DisplayMemberBinding="{Binding Entity.AnotherEntity.AnotherEntityName}" 

这是由于 Fluent Api、xaml 绑定功能或 EntityFrameworkCore 的功能导致数据库的脚手架错误吗?

标签: c#wpfentity-frameworkxamlentity-framework-core

解决方案


我无法访问您的完整代码库,但我认为这是由于EF Core延迟加载功能及其动态代理可能不适用于 WPF 绑定。在您的代码中更改此

_entities = DBContext.Entities.ToList();

对此

_entities = DBContext.Entities
    .Include(x => x.AnotherEntity)
    .ToList();

推荐阅读