开发者

Can we bind dynamic data (dataset or list) to a control in WPF

I have a user control...and the base page(s) which uses this 开发者_JAVA百科user control has a dataset which will be used by the user control.

My dataset is dynamic...means..it can have different number of columns depending upon which page my usercontrol is implemented. Is there any control in wpf which i can use to bind this dataset (without knowing the column information) ...i mean similar to datagrid/gridview in 2.0 ?


Take a look at the WPF toolkit, this contains a Grid which meets your requirements.

This toolkit is built by Microsoft, and part of the Microsoft Shared Source Initiative (see the link I provided).

It's not supported my Microsoft though, so if you get a bug you can use their forums but not call MS support.

If you do want to do it yourself, you can for example write some code that takes a List<T>, you get the generic type, get the properties, iterate over them, write the column headers, iterate over all the items in the list and write all the properties.

I wrote this code a while back to write a List to an HTML table, I hope it's useful:

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>");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜