c# - 将 GridViewColumn 绑定到列表中的空属性
问题描述
- 当我绑定另一个实体的名称时,我的 WPF (netcore3.1-windows) EFCore 5 应用程序中没有初始数据。
- 数据最初确实加载到同一个应用程序中,但使用来自页面加载的 WPF (net40) EFCore 6 应用程序:
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 的功能导致数据库的脚手架错误吗?
解决方案
我无法访问您的完整代码库,但我认为这是由于EF Core延迟加载功能及其动态代理可能不适用于 WPF 绑定。在您的代码中更改此
_entities = DBContext.Entities.ToList();
对此
_entities = DBContext.Entities
.Include(x => x.AnotherEntity)
.ToList();
推荐阅读
- python - 在Python中获取当前文件中类中的所有类方法?
- python - 如何大规模比较多个表中的多个字段?
- laravel - 如何使用单个数据库用户表创建两个单独的站点登录/注册
- perl - Perl 使用系统命令删除文件总是返回成功
- flutter - 使用 pdf 包在 PDF 文档中创建动态表格行
- java - 在 JAVA 中使用 recursion 获得不同的结果 StringBuilder 与 String
- ios - 找不到“EXUpdates/EXUpdatesAppController.h”文件
- github - github问题上的红色/绿色感叹号图标是什么意思?
- docker - 启动 Mayan 时出错“没有名为 'sqlalchemy' 的模块”
- c# - 使用 C#、.net 和 TFS 进行自动化测试的可执行文件