绑定多功能WPF DataGrid

发布于 2024-09-28 19:33:31 字数 608 浏览 3 评论 0原文

如果需要显示来自许多不同数据源的数据(具有不同数量的列以及不同的列标题和类型),您将如何绑定一个 WPF 数据网格?

我当前正在做的是在 ViewModel 中为要在此 DataGrid 中显示的每个不同记录集合创建一个自定义 List() 数据网格列列表。

我循环此列表以设置 DataGrid 列:

foreach (DataGridColumn dgc in dgcSample)
{
  dgc.HeaderStyle = hStyle;
  dgMyDataGrid.Columns.Add(dgc);
}

最后,我使用 ItemsSource 设置项目源:

dgMyDataGrid.ItemsSource = SomeCollection;

这可以工作,但它没有绑定,并且它打破了 MVVM 准则,即 ViewModel 应该不知道特定的 UI 元素,因为它现在具有处理 DataGrid 并托管 DataGridColumn 对象的集合...

有什么想法吗?

How would you go about binding a WPF datagrid that needs to display data from many different datasources with varying number of columns with different column headers and types?

What I'm currently doing is creating a custom List<DataGridColumn>() list of datagrid columns in my ViewModel for each different collection of records to be displayed in this DataGrid.

I Loop over this List to set the DataGrid columns:

foreach (DataGridColumn dgc in dgcSample)
{
  dgc.HeaderStyle = hStyle;
  dgMyDataGrid.Columns.Add(dgc);
}

Finally, I use the ItemsSource to set the source of items:

dgMyDataGrid.ItemsSource = SomeCollection;

This works but it is not binding and it breaks the MVVM guideline that ViewModel should be agnostic to specific UI elements since it now has to deal with the DataGrid and host a collection of DataGridColumn objects...

Any thoughts?

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

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

发布评论

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

评论(3

带刺的爱情 2024-10-05 19:33:31

我认为您必须将 DataGrid 与一个数据源绑定,但您的任务只是创建此数据源。

我将创建一个示例,使用 LINQ 从不同实体创建数据源..

假设您有两个不同的实体:Entity1 和 Entity2,并且每个实体都有共同的 ID:

class Entity1
{
    public int ID { get; set; }
    public string E1Column { get; set; }
}

class Entity2
{
    public int ID { get; set; }
    public string E2Column { get; set; }
}

您可以使用 加入 LINQ,如下所示:

List<Entity1> e1List = new List<Entity1>();
e1List.Add(new Entity1() { ID = 1, E1Column = "E1 a" });
e1List.Add(new Entity1() { ID = 2, E1Column = "E1 b" });

List<Entity2> e2List = new List<Entity2>();
e2List.Add(new Entity2() { ID = 1, E2Column = "E2 a" });
e2List.Add(new Entity2() { ID = 2, E2Column = "E2 b" });

var query = from e1 in e1List
            join e2 in e2List on e1.ID equals e2.ID
            select new { ID = e1.ID, E1Column = e1.E1Column, E2Column = e2.E2Column };

// Bind the DataGrid
dataGrid1.ItemsSource = query.ToList();

祝你好运!

I think you have to bind your DataGrid with one datasource, but your task is just creating this datasource.

I'll create a sample for creating a DataSource from different entities using LINQ..

Assuming you have two different entities: Entity1 and Entity2, and each one has common ID:

class Entity1
{
    public int ID { get; set; }
    public string E1Column { get; set; }
}

class Entity2
{
    public int ID { get; set; }
    public string E2Column { get; set; }
}

You can create the DataSource using Join in LINQ like the following:

List<Entity1> e1List = new List<Entity1>();
e1List.Add(new Entity1() { ID = 1, E1Column = "E1 a" });
e1List.Add(new Entity1() { ID = 2, E1Column = "E1 b" });

List<Entity2> e2List = new List<Entity2>();
e2List.Add(new Entity2() { ID = 1, E2Column = "E2 a" });
e2List.Add(new Entity2() { ID = 2, E2Column = "E2 b" });

var query = from e1 in e1List
            join e2 in e2List on e1.ID equals e2.ID
            select new { ID = e1.ID, E1Column = e1.E1Column, E2Column = e2.E2Column };

// Bind the DataGrid
dataGrid1.ItemsSource = query.ToList();

Good luck!

人事已非 2024-10-05 19:33:31

您是否考虑过使用 http://www.codeproject.com/KB 中显示的 datagrid 派生类/grid/MultiColumnSetDataGrid.aspx

这将允许您在视图中定义多个列集并在它们之间切换。

Have you considered using the datagrid derived class shown in http://www.codeproject.com/KB/grid/MultiColumnSetDataGrid.aspx

This will allow you view to define multiple column sets in your view and switch between them.

放肆 2024-10-05 19:33:31

IMO 正确的方法是将所有数据源封装到一个对象中,这类似于您的自定义 List()。将此称为多功能源

这个新对象将包含列的列表,可能包含添加新源和聚合它们的方法。添加新源时,您可能可以自动管理列列表。

MultifunctionSource 负责提供可绑定的数据源。

该模型将向视图提供 MultifunctionSource 类型的对象。

在视图中,您应该有一个从数据网格派生的新控件,它将了解如何显示 MultifunctionSource 类型的对象。这个新控件在第一个实例中可能会非常简单,因为它可以根据绑定简单地设置其列。

MultifunctionSource 可能应该返回它认为与显示相关的列的列表。它还应该能够返回完整的列列表,以便 UI 能够根据其他条件决定哪些列是相关的;从而保持边界。

IMO the right way of doing this is to encapsulate all the data sources into a single object, that is similar to your custom List<DataGridColumn>(). Call this MultifunctionalSource.

This new object will contain a the list of the columns, probably with methods to add new sources and aggregate them. Possibly when adding a new source you could automatically manage the list of columns.

MultifunctionalSource is responsible for providing a bindable datasource.

The model will provide objects of type MultifunctionalSource to the View.

Within the view you should have a new control derived from datagrid that will understand how to display objects of type MultifunctionalSource. This new control will probably be quite simple in the first instance as it can simply setup its columns based on the binding.

MultifunctionalSource should probably return a list of columns that it thinks are relevant for display. It should also be able to return a complete list of columns to enable the UI to decide which columns are relevant based on other criteria; thus keeping the boundaries.

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