【问题标题】:How do I programmatically insert rows into a Silverlight DataGrid without binding?如何在没有绑定的情况下以编程方式将行插入 Silverlight DataGrid?
【发布时间】:2011-02-28 22:16:15
【问题描述】:

我正在使用通用 Silverlight DataGrid 来显示搜索结果。搜索的“模式”可能因查询而异。

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

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

我想做类似的事情:

List<List<object>> myResults = <voodoo that populates the result list>

myDataGrid.ItemsSource = myResults;

【问题讨论】:

    标签: silverlight datagrid silverlight-4.0


    【解决方案1】:

    是的,这是可能的。这是从 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)
                });
            }
        }
    }
    

    【讨论】:

    • 不,这是我在我的问题中所说的我不想要的。 Customer 是一个强类型类。如果我不知道在编译时从搜索中返回了哪些字段,我该如何为它们构建强类型类?
    【解决方案2】:

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

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

    【讨论】:

    • 我想我要重新设计我的项目以拥有一个强大的 CLR 类型,而不是搞乱动态的东西。对我来说似乎更干净。
    【解决方案3】:

    Colin Eberhardt 为您所描述的问题发布了一个巧妙的解决方案 -

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

    【讨论】:

      猜你喜欢
      • 2010-10-02
      • 2016-06-02
      • 2016-06-10
      • 2011-03-10
      • 2017-12-30
      • 1970-01-01
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多