WPF 数据绑定、CollectionViewSource、INotifyPropertyChanged
当我第一次尝试在 WPF 中做某事时,我对 WPF DataBinding 感到困惑。然后我彻底研究了 MSDN 上的下一个示例: http: //msdn.microsoft.com/en-us/library/ms771319(v=VS.90).aspx
现在,我非常了解如何使用“主从范例”来构建从一个来源获取数据的表单(一张表) - 适用于主零件和详细零件。 我的意思是,例如我有一个包含数据的网格,在网格下方我有一些包含当前行的详细数据的字段。
但是,如果详细数据来自不同但相关的表,该怎么办? 例如:您有一个表“用户”,其中包含“id”和“名称”列。您还有另一个表“Pictures”,其中包含“id”、“Filename”、“UserId”等列。现在,使用主从范例,您必须构建一个表单。每次当您在主视图中选择一行时,您都应该在详细信息中获得所有关联的图片。
正确的做法是什么? 你能给我举个例子吗?
First time when I tried to do something in WPF, I was puzzled with WPF DataBinding. Then I studied thorougly next example on MSDN: http://msdn.microsoft.com/en-us/library/ms771319(v=VS.90).aspx
Now, I quite understand how to use Master-Detail paradigm for a form which takes data from one source (one table) - both for master and detailed parts.
I mean for example I have a grid with data and below the grid I have a few fields with detailed data of current row.
But how to do it, if detailed data comes from different but related tables?
for example: you have a Table 'Users' with columns - 'id' and 'Name'. You also have another table 'Pictures' with columns like 'id','Filename','UserId'. And now, using master-detail paradigm you have to built a form. And every time when you chose a row in a Master you should get all associated pictures in Details.
What is the right way to do it?
Could you please show me an example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您如何访问您的桌子。但我喜欢使用实体框架。
您的“图片”表中似乎有一个外键关系指向您的“用户”主键。
使用 Entity Framework 4 和 .Net 4(在我写这篇文章时都在 RC 中),一切都非常顺利。您只需根据当前数据库生成一个模型即可。这几乎将所有数据库记录映射到您的类。您的每个表都将是模型中的一个集合(我将其称为上下文),因此您可以通过以下方式访问它们:
context.Users 和 context.Pictures
context.Users 将自动拥有一个名为 Pictures 的“导航属性”,它只需是具有指向该用户的外键的特定图片的集合。
因此,您可以将主“网格”绑定到 context.Users 以显示有关您喜欢的用户的任何信息。然后在您的详细信息部分中,您可以将详细信息“列表”绑定到:
(grid.selectedRow as User).Pictures
上面只是伪代码,希望它可以帮助您朝着正确的方向开始。
如果您不想使用实体框架,如果您可以提供有关如何从数据库访问信息的更多信息,我也许可以提供更多有关如何对主详细信息场景进行数据绑定的具体示例。
I am unsure how you are accessing your tables. But I like to use the Entity Framework.
It appears you have a foreign key relationship in your 'pictures' table pointing to your 'User' primary Key.
Using Entity Framework 4 and .Net 4 (both in RC at the time I write this) things are pretty slick. You simply generate a model based on your current database. This pretty much maps all of your database records to classes for you. Each of your tables will be a collection in your model (which I will call context) so you can access them in the following manner:
context.Users and context.Pictures
context.Users will automatically have a "navigation property" called Pictures that simply is a collection of the specific pictures who have foreign keys pointing to that user.
so you would bind your master 'grid' to context.Users to display whatever information about the User you like. Then in your details section you can bind your detail 'list' to:
(grid.selectedRow as User).Pictures
The above is just Pseudo code in hopes that it can get you started in the right direction.
If you don't want to use Entity Framework, if you could provide more information about how you access the information from your database I might be able to provide more of a concrete example of how to databind your master detail scenario.