加入收藏 | 设为首页 | 会员中心 | 我要投稿 大庆站长网 (https://www.0459zz.com/)- 科技、智能边缘云、事件网格、云计算、站长网!
当前位置: 首页 > 综合聚焦 > Windows > 正文

如何将列表转换为ObservableCollection?

发布时间:2020-07-16 03:06:39 所属栏目:Windows 来源:互联网
导读:我是一个java开发人员,新到C#silverlight。 在这个类中,我想将产品(List)转换为ObservableCollection。 using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq;namespace

我是一个java开发人员,新到C#silverlight。
在这个类中,我想将产品(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",IMG_PATH + "FundSecurity_100.jpg"));
      DataCollection.Add(new Product("Fundamentals of SQL Server eBook",IMG_PATH + "FundSQL_100.jpg"));
      DataCollection.Add(new Product("Fundamentals of VB.NET eBook",IMG_PATH + "FundVBNet_100.jpg"));
      DataCollection.Add(new Product("Fundamentals of .NET eBook",IMG_PATH + "FundDotNet_100.jpg"));
      DataCollection.Add(new Product("Architecting ASP.NET eBook",IMG_PATH + "ArchASPNET_100.jpg"));
      DataCollection.Add(new Product("PDSA .NET Productivity Framework",Convert.ToDecimal(2500),IMG_PATH + "framework.jpg"));

      return DataCollection;
    }
  }
}

我该怎么办来解决?还是需要创建一个新的类?

您的产品类不应该继承任何东西。
public class Products

通过Product类的DataCollection属性访问集合中的所有项目。例如,

Products myProducts = new Products();
ObservableCollection<Product> myData = myProducts.DataCollection;

它还取决于您如何使用产品。你可以完全不用这个课,然后做一些类似的事情:

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

(编辑:大庆站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读