Linq to SQL EntitySet 绑定 MVVM 方式

发布于 2024-08-15 10:56:01 字数 974 浏览 3 评论 0原文

在 WPF 应用程序中,我使用 LINQ to SQL 类(由 SQL Metal 创建,从而实现 POCO)。

假设我有一个用户表和图片表。这些图片实际上是由一张图片创建的,它们之间的差异可能是大小、颜色……

所以每个用户可能有不止一张图片,所以关联是1:N(用户:图片)。

我的问题

a)我如何以MVVM方式将图片控件绑定到EntitySet中的一张图片(我将拍摄一张特定图片),以显示它向上?

b) 每次用户更改其图片时,整个 EntitySet 都应被丢弃,并应添加新创建的图片。这是正确的方法吗?

例如,

//create the 1st piture object
UserPicture1 = new UserPicture();
UserPicture1.Description = "... some description.. ";
USerPicture1.Image = imgBytes; //array of bytes


//create the 2nd piture object
UserPicture2 = new UserPicture();
UserPicture2.Description = "... another description.. ";
UserPicture2.Image = DoSomethingWithPreviousImg(imgBytes); //array of bytes


//Assuming that the entityset is called Pictures
//add these pictures to the corresponding user
User.Pictures.Add(UserPicture1);
User.Pictures.Add(UserPicture2);


//save changes 
datacontext.Save()

提前致谢

In a WPF application i'm using LINQ to SQL classes (created by SQL Metal, thus implementing POCOs).

Let's assume i have a table User and a Table Pictures. These pictures are actually created from one picture, the difference between them may be the size, coloring,...

So every user may has more than one Pictures, so the association is 1:N (User:Pictures).

My problems:

a) how do i bind, in a MVVM manner, a picture control to one picture (i will take one specific picture) in the EntitySet, to show it up?

b) everytime a user changes her picture the whole EntitySet should be thrown away and the newly created Picture(s) should be a added. Is this the correct way?

e.g.

//create the 1st piture object
UserPicture1 = new UserPicture();
UserPicture1.Description = "... some description.. ";
USerPicture1.Image = imgBytes; //array of bytes


//create the 2nd piture object
UserPicture2 = new UserPicture();
UserPicture2.Description = "... another description.. ";
UserPicture2.Image = DoSomethingWithPreviousImg(imgBytes); //array of bytes


//Assuming that the entityset is called Pictures
//add these pictures to the corresponding user
User.Pictures.Add(UserPicture1);
User.Pictures.Add(UserPicture2);


//save changes 
datacontext.Save()

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2024-08-22 10:56:01

我有两个选择给你(但还有更多)。

  1. 绑定到图片并使用您自己的转换器选择合适的一个。
  2. 创建一个 ViewModel 以根据您的视图需求包装模型,公开 Picture 属性并绑定到它。

如果您需要更多详细信息,请发表评论。

干杯,安瓦卡

I have two options for you (but there are many more).

  1. Bind to Pictures and use your own converter to choose proper one.
  2. Create a ViewModel to wrap model according to your view needs, expose Picture propery, and bind to it.

Please comment if you need more details.

Cheers, Anvaka

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文