结合多功能WPF的DataGrid [英] Binding multifunctional WPF DataGrid

查看:140
本文介绍了结合多功能WPF的DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您会如何去绑定一个WPF的DataGrid,需要有不同的列数不同的列标题和类型,以显示来自不同数据源的数据?

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?

我M目前正在做的是创建一个自定义列表<一个DataGridColumn方式>()在我的视图模型为每个不同的集合将显示在这个DataGrid中记录的数据网格的列清单

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.

我遍历这个列表来设置DataGrid列:

I Loop over this List to set the DataGrid columns:

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



最后,我用的ItemsSource设置项目的源代码:

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

dgMyDataGrid.ItemsSource = SomeCollection;

这工作,但它不具有约束力,它打破了MVVM方针的视图模型应该是不可知的具体UI因为它的元素现在有对付的DataGrid 与主机的DataGridColumn 对象的集合...

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

有什么想法?

推荐答案

你有没有考虑使用中显示的数据网格派生类< A HREF =http://www.codeproject.com/KB/grid/MultiColumnSetDataGrid.aspx相对=nofollow> http://www.codeproject.com/KB/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.

这篇关于结合多功能WPF的DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆