WPF DataGrid 父子数据
我是 WPF/C#/NET 的新手,但我一直在通过编写一些小练习来学习。
无论如何,我被困住了。我在这里和谷歌上搜索过,但找不到答案,或者更正确地说,找不到我能理解的答案。
我的问题是这样......使用实体框架我有两个表。一份用于员工详细信息,一份用于公司详细信息。员工为 0 或 1 家公司工作。
我想通过 WPF/XAML 定义一个数据网格来导航员工。但在每个员工行中,我想显示他们工作的公司名称(如果有关系)或在没有相关公司记录的情况下显示“失业”。
我没有提供表格的详细信息,因为这实际上并不重要 - 问题是在单个数据网格中显示来自父/子关系的串联信息。
我不知道解决这类问题的最佳方法是什么,我假设是 WPF/DataGrid,所以我非常感谢有关如何去做、绑定(假设是 WPF)甚至 WPF/DataGrid 的示例的帮助。 XAML
提前致谢。
I'm new to WPF/C#/NET but I have been learning by way of coding some small exercises.
Anyway, I'm stuck. I have seached on here and google and can't find the answer or maybe more correctly can't find an answer I can make sense of.
My problem is this... using the Entity Framework I have two tables. One for Employee details and one for Company details. Employees work for 0 or 1 Company's.
I would like to, via WPF/XAML, define a datagrid to navigate Employees. But within each employee row I would like to show the name of the Company they work for (if there is a relationship) or "Unemployed" in the cases where there is no related Company record.
I have not given details of the tables as it really doesnt matter - the problem is displaying concatentated information from parent/child relationships in a single datagrid.
I dont know what the best approach to this kind of problem is, I'm assuming WPF/DataGrid, so I would really appreciate help on how to go about doing it, the binding (assuming WPF) or even an example of the WPF/XAML
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有很多方法可以实现此目的 - 您可能尝试的一种方法是创建一个封装要显示的数据的视图模型 - 例如
,然后,给定一个
IEnumerable
您可以投影您的员工数据进入此视图模型并将其设置为 DataGrid 的 ItemsSource - 例如,您通常会在此处通过 xaml 绑定设置 ItemsSource,而不是直接在代码中设置它,但这将涉及使用设置为视图的 DataContext 的父 ViewModel我试图让事情变得简单。
使用 DataGrid 实现此目的的另一种方法是放弃使用视图模型,直接绑定到
IEnumerable
集合并显式设置列绑定 - 例如请注意,在第二个示例中,如果员工没有关联公司,则“公司名称”列中不会显示“失业”。
编辑:为了阐明从绑定到视图的“主”视图模型上的属性设置网格的项目源的要点,您可以创建一个表示整个当前视图的 ViewModel 类。例如,
您可以将其设置为 Window / UserControl 的 DataContext。
例如,在简单的情况下,您可以在窗口的构造函数中执行此操作(尽管对于 WPF 应用程序来说,在可能的情况下,对数据库的调用实际上应该是异步的)。
然后,您可以像这样在 Grid 上设置绑定:
主窗口的数据上下文将用于其中包含的所有子控件,除非您在子控件上显式设置 DataContext。当您拥有包含许多属性和命令的更复杂的 ViewModel 时,此技术会变得非常有用。如果您有兴趣了解更多信息,您应该查看 MVVM 模式以及 Prism / Caliburn 框架中的一个或两个。
There are many ways to accomplish this - one way you might try is to create a View Model that encapsulates the data you want to display - e.g.
Then, given an
IEnumerable<Employee>
you can project your employee data into this view model and set it as the ItemsSource of your DataGrid - e.g.You would normally set the ItemsSource via a xaml binding here rather than setting it directly in code but that would involve the use of a parent ViewModel set as the DataContext of the View and I'm trying to keep things simple.
Another way to accomplish this with a DataGrid would be to forgo the use of a View Model, bind directly to an
IEnumerable<Employee>
collection and set the column bindings explicitly - e.g.Note that with the second example, you won't get "Unemployed" appearing in the Company Name column where there is no associated company for an employee.
EDIT: To clarify the point about setting the items source for your Grid from a property on a 'main' view model bound to the View, you might create a ViewModel class that represents the whole of the current view. e.g.
You'd set this as the DataContext for the Window / UserControl.
e.g. in simple cases you could do this in the constructor for your Window (although calls to the database should really be asynchronous for WPF apps where possible).
Then you can set up the binding on your Grid like this:
The data context for the main window is then used for all the child controls contained therein except where you explicitly set the DataContext on a child control. This technique becomes useful where you have more complex ViewModels with many properties and commands. If you are interested in finding out more you should take a look at the MVVM pattern and either or both of the Prism / Caliburn frameworks.