代码之家  ›  专栏  ›  技术社区  ›  Jordan Parmer

如何在不绑定的情况下以编程方式将行插入Silverlight DataGrid?

  •  2
  • Jordan Parmer  · 技术社区  · 15 年前

    我正在使用一个通用的Silverlight数据报来显示搜索结果。搜索的“模式”因查询而异。

    为了适应这种情况,我尝试动态地填充数据报。我可以显式设置列,但在设置itemsource时遇到问题。所有msdn示例都将itemsource设置为具有强类型的集合(例如,具有与架构匹配的公共属性的自定义类型)。然后,DataGrid使用反射来清除强类型中与列匹配的公共属性。

    因为我的搜索结果是动态的,所以我不能创建一个强类型来表示返回的内容。只要每个列表中的对象数与列数匹配,我就不能给数据报一个任意的对象列表吗?有人知道这是否可行吗?

    我想做类似的事情:

    List<List<object>> myResults = <voodoo that populates the result list>
    
    myDataGrid.ItemsSource = myResults;
    
    3 回复  |  直到 15 年前
        1
  •  1
  •   Jordan Parmer    15 年前

    下面的文章让我接近我想要的东西: http://blogs.msdn.com/b/scmorris/archive/2008/04/14/defining-silverlight-datagrid-columns-at-runtime.aspx

    本质上,您必须具有可绑定属性。不能只根据任意项目列表创建行。我偶然发现了几个开源项目,它们通过在运行时使用反射来构建CLR类型,然后绑定到这些类型来解决这个问题。

        2
  •  1
  •   icysharp    15 年前

    科林·埃伯哈特为你描述的问题提出了一个巧妙的解决方案。-

    http://www.scottlogic.co.uk/blog/colin/2010/03/binding-a-silverlight-3-datagrid-to-dynamic-data-via-idictionary-updated/

        3
  •  0
  •   TheGeekYouNeed    15 年前

    是的,这是可能的。这是从msdn刷来的样品 使用系统; 使用system.collections.generic; 使用system.windows.controls;

    namespace DataGridSnippets
    {
        public partial class Page : UserControl
        {
            public Page()
            {
                InitializeComponent();
    
                // Set the ItemsSource to autogenerate the columns.
                dataGrid1.ItemsSource = Customer.GetSampleCustomerList();
                dataGrid3.ItemsSource = Customer.GetSampleCustomerList();
                dataGrid4.ItemsSource = Customer.GetSampleCustomerList();
                dataGrid5.ItemsSource = Customer.GetSampleCustomerList();
            }
        } 
    
        public class Customer
        {
            public String FirstName { get; set; }
            public String LastName { get; set; }
            public String Address { get; set; }
            public Boolean IsNew { get; set; }
    
            // A null value for IsSubscribed can indicate 
            // "no preference" or "no response".
            public Boolean? IsSubscribed { get; set; }
    
            public Customer(String firstName, String lastName, 
                String address, Boolean isNew, Boolean? isSubscribed)
            {
                this.FirstName = firstName;
                this.LastName = lastName;
                this.Address = address;
                this.IsNew = isNew; 
                this.IsSubscribed = isSubscribed;
            }
    
            public static List<Customer> GetSampleCustomerList()
            {
                return new List<Customer>(new Customer[4] {
                    new Customer("A.", "Zero", 
                        "12 North Third Street, Apartment 45", 
                        false, true), 
                    new Customer("B.", "One", 
                        "34 West Fifth Street, Apartment 67", 
                        false, false),
                    new Customer("C.", "Two", 
                        "56 East Seventh Street, Apartment 89", 
                        true, null),
                    new Customer("D.", "Three", 
                        "78 South Ninth Street, Apartment 10", 
                        true, true)
                });
            }
        }
    }