list 如何将列表转换为 ObservableCollection?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9677439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Convert List to ObservableCollection?
提问by Albert.Qing
I am a java Developer,new to C# silverlight. in this class I want to Convert the Products(List) to ObservableCollection.
我是一名 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;
}
}
}
what should I do to fix it? or need to create a new class?
我该怎么做才能解决它?还是需要创建一个新类?
采纳答案by Brad Rem
Your Products class shouldn't inherit anything.
您的 Products 类不应该继承任何东西。
public class Products
Accessing all the items in your collection is done through the DataCollection property of the Product class. For example,
访问集合中的所有项目是通过 Product 类的 DataCollection 属性完成的。例如,
Products myProducts = new Products();
ObservableCollection<Product> myData = myProducts.DataCollection;
It also depends on how you want to use Products. You may be able to totally do away with this class and then do something like:
这也取决于您想如何使用产品。您可以完全取消这个类,然后执行以下操作:
ObservableCollection<Product> Products = new ObservableCollection<Product>();
Products.Add(new Product("Haystack Code Generator for .NET", 799, IMG_PATH + "Haystack.jpg"));
// etc...
回答by Ahad aghapour
There is a constructor in ObservableCollection
that does this:
有一个构造函数ObservableCollection
可以做到这一点:
ObservableCollection<T> oc = new ObservableCollection<T>(List<T> list);
回答by William Melani
You can make a extension method that will let you convert any type of List
into an ObservableCollection
easily.
您可以制作一个扩展方法,让您可以轻松地将任何类型List
转换为ObservableCollection
。
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;
}
}
For example:
例如:
Products p = new Products();
//add your products
var collection = p.ToObservableCollection(); //use the extension method.
回答by Jones
The class Products is a list. In BuildCollection you access this with the Add method. You don't need an auxiliary structure in my opinion.
产品类是一个列表。在 BuildCollection 中,您可以使用 Add 方法访问它。在我看来,您不需要辅助结构。
To convert list to ObservableCollection use
将列表转换为 ObservableCollection 使用
this.Select<Product, ObservableCollection>(p => p)
method on collection.
收集方法。