WPF DataGrid 父子数据

发布于 2024-09-12 03:07:04 字数 436 浏览 1 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(1

朕就是辣么酷 2024-09-19 03:07:04

有很多方法可以实现此目的 - 您可能尝试的一种方法是创建一个封装要显示的数据的视图模型 - 例如

public class EmployeeViewModel
{
    private readonly Employee _employee;

    public EmployeeViewModel(Employee employee)
    {
        _employee = employee;
    }

    public string Name { get { return _employee.Name; } }
    public string CompanyName { get { return _employee.Company == null ? "Unemployed" : _employee.Company.CompanyName; } }
}

,然后,给定一个 IEnumerable 您可以投影您的员工数据进入此视图模型并将其设置为 DataGrid 的 ItemsSource - 例如,

IEnumerable<Employee> employees = GetEmployeesFromDatabase();    
DataGrid1.ItemsSource = employees.Select(x => new EmployeeViewModel(x));

您通常会在此处通过 xaml 绑定设置 ItemsSource,而不是直接在代码中设置它,但这将涉及使用设置为视图的 DataContext 的父 ViewModel我试图让事情变得简单。

使用 DataGrid 实现此目的的另一种方法是放弃使用视图模型,直接绑定到 IEnumerable 集合并显式设置列绑定 - 例如请

<DataGrid ItemsSource="{Binding Employees}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Employee Name" Binding="{Binding Name}" />
        <DataGridTextColumn Header="Company Name" Binding="{Binding Company.Name}" />
    </DataGrid.Columns>
</DataGrid>

注意,在第二个示例中,如果员工没有关联公司,则“公司名称”列中不会显示“失业”。

编辑:为了阐明从绑定到视图的“主”视图模型上的属性设置网格的项目源的要点,您可以创建一个表示整个当前视图的 ViewModel 类。例如,

public class MainViewModel
{
    private readonly IEnumerable<Employee> _employees;

    public MainViewModel(IEnumerable<Employee> employees)
    {
        _employees = employees;
    }

    public IEnumerable<Employee> Employees
    {
        get { return _employees; }
    }
}

您可以将其设置为 Window / UserControl 的 DataContext。

例如,在简单的情况下,您可以在窗口的构造函数中执行此操作(尽管对于 WPF 应用程序来说,在可能的情况下,对数据库的调用实际上应该是异步的)。

 public MainWindow()
 {
     InitializeComponent();
     DataContext = new MainViewModel(GetAllEmployees());
 }

然后,您可以像这样在 Grid 上设置绑定:

<DataGrid ItemsSource="{Binding Employees}" ...

主窗口的数据上下文将用于其中包含的所有子控件,除非您在子控件上显式设置 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.

public class EmployeeViewModel
{
    private readonly Employee _employee;

    public EmployeeViewModel(Employee employee)
    {
        _employee = employee;
    }

    public string Name { get { return _employee.Name; } }
    public string CompanyName { get { return _employee.Company == null ? "Unemployed" : _employee.Company.CompanyName; } }
}

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.

IEnumerable<Employee> employees = GetEmployeesFromDatabase();    
DataGrid1.ItemsSource = employees.Select(x => new EmployeeViewModel(x));

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.

<DataGrid ItemsSource="{Binding Employees}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Employee Name" Binding="{Binding Name}" />
        <DataGridTextColumn Header="Company Name" Binding="{Binding Company.Name}" />
    </DataGrid.Columns>
</DataGrid>

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.

public class MainViewModel
{
    private readonly IEnumerable<Employee> _employees;

    public MainViewModel(IEnumerable<Employee> employees)
    {
        _employees = employees;
    }

    public IEnumerable<Employee> Employees
    {
        get { return _employees; }
    }
}

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).

 public MainWindow()
 {
     InitializeComponent();
     DataContext = new MainViewModel(GetAllEmployees());
 }

Then you can set up the binding on your Grid like this:

<DataGrid ItemsSource="{Binding Employees}" ...

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.

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