gpt4 book ai didi

c# - 如何填充 ItemsControl 中的外键字段?

转载 作者:行者123 更新时间:2023-11-29 11:21:04 24 4
gpt4 key购买 nike

我的数据库中有两个表。员工和照片。

员工拥有一般员工凭证和一个名为 PictureId 的外键,该外键引用图片表。

Employee table

图片表包含要在我的应用程序中使用的所有员工图像(以及其他一些杂项图像)

Picture table

因此,在我的 WPF 应用程序中,我有一个带有自己的 ViewModel 的 UserControl。 ViewModel 从数据库获取员工信息(我使用的是 Entity-Framework 6 数据库优先方法)。

public class MyViewModel
{
private databaseEntities context;
public List<employee> EmployeeList { get; set; }

// Constructor
public MyViewModel()
{
context = new databaseEntities();
var queryEmployees = context.employees;
EmployeeList = new List<employee>(queryEmployees);
}
}

View (用户控件)将此信息排列为卡片。

<ItemsControl ItemsSource="{Binding EmployeeList}">
<Grid...>
<Image Source="/assets/images/CardBackground.PNG" Stretch="Fill" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
<!-- How would I fetch the corresponding image data meant to go below? -->
<Image Source="{Binding Path= ??, Converter={StaticResource PictureConverter}}"/>
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</Grid>
</ItemsControl>

这就是我的问题所在。我可以使用上面的代码从 Employee 表中检索信息。但当谈到获取他们的展示图片时,我遇到了困难。

我最初假设员工模型首先必须以与类加载基类相同的方式加载它的依赖项,但显然它并不完全按照这种方式工作。

创建并填充单独的列表似乎也没有任何效果。

如何在同一个 ItemsControl 中加载外部表字段?

最佳答案

如果您使用Entity FrameWork,那么Employee类中必须有导航属性。

假设该属性是 Picture,那么您可以执行类似的操作。

<ItemsControl ItemsSource="{Binding EmployeeList}">
<Grid...>
<Image Source="/assets/images/CardBackground.PNG" Stretch="Fill" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
<!-- How would I fetch the corresponding image data meant to go below? -->
<Image Source="{Binding Path=Employee.Picture.PicturePath, Converter={StaticResource PictureConverter}}"/>
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</Grid>
</ItemsControl>

我假设导航属性的名称和图片实体的属性。请提供 Employee 类和 Picture 类的定义,以便我可以根据需要编辑我的答案,否则您将从上面的代码中了解该怎么做

关于c# - 如何填充 ItemsControl 中的外键字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38927709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com