我们可以将动态数据(数据集或列表)绑定到WPF中的控件

Rel*_*ity 2 data-binding wpf dynamic-data

我有一个用户控件...并且使用此用户控件的基页具有将由用户控件使用的数据集.

我的数据集是动态的......意思是......根据我的usercontrol实现的页面,它可以有不同的列数.在wpf中是否有任何控件可用于绑定此数据集(不知道列信息)...我的意思是类似于2.0中的datagrid/gridview?

Sna*_*ake 5

看一下WPF工具包,它包含一个满足您要求的Grid.

此工具包由Microsoft构建,是Microsoft Shared Source Initiative的一部分(请参阅我提供的链接).

它不支持我的微软,所以如果你得到一个bug,你可以使用他们的论坛,但不能打电话给MS支持.

如果你想自己想做,你可以编写一些代码来获取a List<T>,获得泛型类型,获取属性,迭代它们,编写列标题,迭代列表中的所有项目并写入所有属性.

我写了这段代码一段时间后将List写入HTML表格,我希望它有用:

public void PrintList<T>(List<T> list)
{
    if(null!=list.FirstOrDefault())
    {
       Type t = typeof(list[0]);
       PropertyInfo[] properties = t.GetProperties();

       // properties = list of all properties.

       print("<table><tr>");

       foreach(var property in properties)
       {
          // print property title
          print(string.Format("<th>{0}</th>", property.Name));
       }   


       print("</tr>");

       foreach(var item in list)
       {
          print("<tr>");
          foreach(var property in properties)
          {
               var propertyValue = t.InvokeMember(property.Name, BindingFlags.GetProperty, null, item, new object[] {});
               print(string.Format("<td>{0}</td>", propertyValue));
          }

          print("</tr>");
       }

       print("</table>");
    }
}
Run Code Online (Sandbox Code Playgroud)