Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net c中将可观察集合绑定到列表框_C#_Asp.net_Listbox - Fatal编程技术网

C# 在asp.net c中将可观察集合绑定到列表框

C# 在asp.net c中将可观察集合绑定到列表框,c#,asp.net,listbox,C#,Asp.net,Listbox,这是我的密码 ObservableCollection<product> pr = new ObservableCollection<product>(); protected void Page_Load(object sender, EventArgs e) { product p = new product(); p.PhoneMake = "Apple";

这是我的密码

                         ObservableCollection<product> pr = new ObservableCollection<product>();
    protected void Page_Load(object sender, EventArgs e)
    {
        product p = new product();
        p.PhoneMake = "Apple";
        p.PhoneModel = "4S";
        p.Price = 40;
        pr.Add(p);

        product q = new product();
        p.PhoneMake = "Apple";
        p.PhoneModel = "5S";
        p.Price = 80;
        pr.Add(q);

        lstbxProd.DataSource = pr;

您需要订阅可观察集合的CollectionChanged事件。 像这样的事情应该让你开始

         ObservableCollection<Product> products = new ObservableCollection<Product>();
           //Replace the lst with your control
                var lst = new List<Product>();
            products.CollectionChanged += (sender, eve) =>
            {
                switch (eve.Action)
                {

                    case NotifyCollectionChangedAction.Add:
                        //add to your control here    
                        lst.Add((Product) eve.NewItems[0]);
                        break;
                    case NotifyCollectionChangedAction.Remove:
                         //add implementation here
                        break;
                    case NotifyCollectionChangedAction.Replace:
                        break;
                    case NotifyCollectionChangedAction.Move:
                        break;
                    case NotifyCollectionChangedAction.Reset:
                        break;
                }
            };

请共享您的xaml代码
         ObservableCollection<Product> products = new ObservableCollection<Product>();
           //Replace the lst with your control
                var lst = new List<Product>();
            products.CollectionChanged += (sender, eve) =>
            {
                switch (eve.Action)
                {

                    case NotifyCollectionChangedAction.Add:
                        //add to your control here    
                        lst.Add((Product) eve.NewItems[0]);
                        break;
                    case NotifyCollectionChangedAction.Remove:
                         //add implementation here
                        break;
                    case NotifyCollectionChangedAction.Replace:
                        break;
                    case NotifyCollectionChangedAction.Move:
                        break;
                    case NotifyCollectionChangedAction.Reset:
                        break;
                }
            };