如何将List转换为ObservableCollection?

Alb*_*ing 18 listbox list windows-phone-7

我是一名java开发人员,是C#silverlight的新手.在这个类中,我想将Products(List)转换为ObservableCollection.

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;

namespace WPListBoxImage
{
/**It seems not work,if I just change List<Product> to ObservableCollection<Product>
  public class Products : List<Product>
  {
    public Products()
    {
      BuildCollection();

    }

    private const string IMG_PATH = "../Images/";

    public ObservableCollection<Product> DataCollection { get; set; }

    public ObservableCollection<Product> BuildCollection()
    {
      DataCollection = new ObservableCollection<Product>();

      DataCollection.Add(new Product("Haystack Code Generator for .NET", 799, IMG_PATH + "Haystack.jpg"));
      DataCollection.Add(new Product("Fundamentals of N-Tier eBook", Convert.ToDecimal(19.95), IMG_PATH + "FundNTier_100.jpg"));
      DataCollection.Add(new Product("Fundamentals of ASP.NET Security eBook", Convert.ToDecimal(19.95), IMG_PATH + "FundSecurity_100.jpg"));
      DataCollection.Add(new Product("Fundamentals of SQL Server eBook", Convert.ToDecimal(19.95), IMG_PATH + "FundSQL_100.jpg"));
      DataCollection.Add(new Product("Fundamentals of VB.NET eBook", Convert.ToDecimal(19.95), IMG_PATH + "FundVBNet_100.jpg"));
      DataCollection.Add(new Product("Fundamentals of .NET eBook", Convert.ToDecimal(19.95), IMG_PATH + "FundDotNet_100.jpg"));
      DataCollection.Add(new Product("Architecting ASP.NET eBook", Convert.ToDecimal(19.95), IMG_PATH + "ArchASPNET_100.jpg"));
      DataCollection.Add(new Product("PDSA .NET Productivity Framework", Convert.ToDecimal(2500), IMG_PATH + "framework.jpg"));

      return DataCollection;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决这个问题?还是需要创建一个新类?

Aha*_*our 72

这样做有一个构造函数ObservableCollection:

ObservableCollection<T> oc = new ObservableCollection<T>(List<T> list);
Run Code Online (Sandbox Code Playgroud)


Bra*_*Rem 7

您的Products类不应继承任何内容.

public class Products
Run Code Online (Sandbox Code Playgroud)

访问集合中的所有项目都是通过Product类的DataCollection属性完成的.例如,

Products myProducts = new Products();
ObservableCollection<Product> myData = myProducts.DataCollection;
Run Code Online (Sandbox Code Playgroud)

它还取决于您希望如何使用产品.你可以完全取消这个课程,然后做一些事情:

ObservableCollection<Product> Products = new ObservableCollection<Product>();
Products.Add(new Product("Haystack Code Generator for .NET", 799, IMG_PATH + "Haystack.jpg"));
// etc...
Run Code Online (Sandbox Code Playgroud)


Wil*_*ani 5

你可以做一个扩展方法,可以让你转换任何类型的ListObservableCollection容易.

public static class CollectionUtils
{
    public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> thisCollection)
    {
        if (thisCollection == null) return null;
        var oc = new ObservableCollection<T>();

        foreach (var item in thisCollection)
        {
            oc.Add(item);
        }

        return oc;
    }
}
Run Code Online (Sandbox Code Playgroud)

例如:

Products p = new Products();
//add your products

var collection = p.ToObservableCollection(); //use the extension method.
Run Code Online (Sandbox Code Playgroud)