绑定多功能WPF DataGrid
如果需要显示来自许多不同数据源的数据(具有不同数量的列以及不同的列标题和类型),您将如何绑定一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您必须将
DataGrid
与一个数据源绑定,但您的任务只是创建此数据源。我将创建一个示例,使用 LINQ 从不同实体创建数据源..
假设您有两个不同的实体:Entity1 和 Entity2,并且每个实体都有共同的 ID:
您可以使用 加入 LINQ,如下所示:
祝你好运!
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:
You can create the DataSource using Join in LINQ like the following:
Good luck!
您是否考虑过使用 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.
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 thisMultifunctionalSource
.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.