如何将列表转换为ObservableCollection?
发布时间:2020-07-16 03:03:24  所属栏目:Windows  来源:互联网 
            导读:我是一个java开发人员,新到C#silverlight。 在这个类中,我想将产品(List)转换为ObservableCollection。 using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq;namespace
                
                
                
            | 
                         我是一个java开发人员,新到C#silverlight。 
  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...                        (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
相关内容
- Windows调试工具入门 — windebug
 - Windows Community Toolkit 3.0 新功能 在WinForms 和 WPF
 - windows – 为什么没有64位版本的VS2013?
 - windows+tomcat+https(免费证书)
 - 电子在Windows bash上不能开箱即用
 - 获取与Windows Vista上的C#.Net连接的无线网络的SSID
 - windows-8 – 屏幕关闭时,WinRT应用程序可以继续运行吗?
 - 找出占用Installer 目录空间的元凶
 - windows – 我可以将16位.exe程序转换为64位.exe吗?
 - win7使用远程桌面 连接 windows服务器
 
