gpt4 book ai didi

list - 如何将列表转换为ObservableCollection?

转载 作者:行者123 更新时间:2023-12-03 07:28:02 24 4
gpt4 key购买 nike

我是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;
}
}
}

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

最佳答案

您的Products类不应继承任何东西。

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", 799, IMG_PATH + "Haystack.jpg"));
// etc...

关于list - 如何将列表转换为ObservableCollection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9677439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com