ObservableCollection<T> 类

https://msdn.microsoft.com/zh-cn/library/ms668604(VS.95).aspx

表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。

 

 

命名空间:  System.Collections.ObjectModel
程序集:  System.Windows(在 System.Windows.dll 中)
语法
 
 
C#
VB
 
public class ObservableCollection<T> : Collection<T>, 
	INotifyCollectionChanged, INotifyPropertyChanged

类型参数
T

集合中的项的类型。

ObservableCollection<T> 类型公开以下成员。

 

构造函数
 
 

 

显示: 继承 保护
 名称说明
公共方法受 Silverlight for Windows Phone 支持ObservableCollection<T>()初始化 ObservableCollection<T> 类的新的空实例。
公共方法受 Silverlight for Windows Phone 支持ObservableCollection<T>(IEnumerable<T>)初始化 ObservableCollection<T> 类的新实例,并用从指定集合中复制的项来填充该实例。
公共方法受 Silverlight for Windows Phone 支持ObservableCollection<T>(List<T>)

初始化 ObservableCollection<T> 类的新实例,并用从指定列表中复制的项来填充该实例。

 

 

属性
 
 
显示: 继承 保护
 名称说明
公共属性受 Silverlight for Windows Phone 支持Count获取 Collection<T> 中实际包含的元素数。 (继承自 Collection<T>。)
公共属性受 Silverlight for Windows Phone 支持Item获取或设置位于指定索引处的元素。 (继承自 Collection<T>。)
受保护的属性受 Silverlight for Windows Phone 支持Items获取 Collection<T> 周围的 IList<T> 包装。 (继承自 Collection<T>。)
页首

 

方法
 
 
显示: 继承 保护
 名称说明
公共方法受 Silverlight for Windows Phone 支持Add将对象添加到 Collection<T> 的结尾处。 (继承自 Collection<T>。)
公共方法受 Silverlight for Windows Phone 支持Clear Collection<T> 中移除所有元素。 (继承自 Collection<T>。)
受保护的方法受 Silverlight for Windows Phone 支持ClearItems从集合中移除所有项。 (重写 Collection<T>.ClearItems()。)
公共方法受 Silverlight for Windows Phone 支持Contains确定某元素是否在 Collection<T> 中。 (继承自 Collection<T>。)
公共方法受 Silverlight for Windows Phone 支持CopyTo从目标数组的指定索引处开始将整个 Collection<T> 复制到兼容的一维 Array (继承自 Collection<T>。)
公共方法受 Silverlight for Windows Phone 支持Equals(Object)确定指定的 Object 是否等于当前的 Object (继承自 Object。)
受保护的方法受 Silverlight for Windows Phone 支持Finalize允许对象在垃圾回收对 Object 进行回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
公共方法受 Silverlight for Windows Phone 支持GetEnumerator返回循环访问 Collection<T> 的枚举数。 (继承自 Collection<T>。)
公共方法受 Silverlight for Windows Phone 支持GetHashCode用作特定类型的哈希函数。 (继承自 Object。)
公共方法受 Silverlight for Windows Phone 支持GetType获取当前实例的 Type (继承自 Object。)
公共方法受 Silverlight for Windows Phone 支持IndexOf搜索指定的对象,并返回整个 Collection<T> 中第一个匹配项的从零开始的索引。 (继承自 Collection<T>。)
公共方法受 Silverlight for Windows Phone 支持Insert将元素插入 Collection<T> 的指定索引处。 (继承自 Collection<T>。)
受保护的方法受 Silverlight for Windows Phone 支持InsertItem将一项插入集合中指定索引处。 (重写 Collection<T>.InsertItem(Int32, T)。)
受保护的方法受 Silverlight for Windows Phone 支持MemberwiseClone创建当前 Object 的浅表副本。 (继承自 Object。)
受保护的方法受 Silverlight for Windows Phone 支持OnCollectionChanged引发带有所提供的事件数据的 CollectionChanged 事件。
受保护的方法受 Silverlight for Windows Phone 支持OnPropertyChanged引发带有提供的参数的 PropertyChanged 事件。
公共方法受 Silverlight for Windows Phone 支持Remove Collection<T> 中移除特定对象的第一个匹配项。 (继承自 Collection<T>。)
公共方法受 Silverlight for Windows Phone 支持RemoveAt移除 Collection<T> 的指定索引处的元素。 (继承自 Collection<T>。)
受保护的方法受 Silverlight for Windows Phone 支持RemoveItem从集合中移除指定索引位置的项。 (重写 Collection<T>.RemoveItem(Int32)。)
受保护的方法受 Silverlight for Windows Phone 支持SetItem替换指定索引处的项。 (重写 Collection<T>.SetItem(Int32, T)。)
公共方法受 Silverlight for Windows Phone 支持ToString返回表示当前对象的字符串。 (继承自 Object。)
页首

 

事件
 
 
显示: 继承 保护
 名称说明
公共事件受 Silverlight for Windows Phone 支持CollectionChanged当集合中的某个项更改或者整个集合更改时发生。
受保护的事件受 Silverlight for Windows Phone 支持PropertyChanged当集合中单个项的属性更改时发生。
页首

 

显示: 继承 保护
 名称说明
显式接口实现私有方法受 Silverlight for Windows Phone 支持ICollection.CopyTo从特定的 Array 索引处开始,将 ICollection 的元素复制到一个 Array 中。 (继承自 Collection<T>。)
显式接口实现私有属性受 Silverlight for Windows Phone 支持ICollection<T>.IsReadOnly获取一个值,该值指示 ICollection<T> 是否为只读。 (继承自 Collection<T>。)
显式接口实现私有属性受 Silverlight for Windows Phone 支持ICollection.IsSynchronized获取一个值,该值指示是否同步对 ICollection 的访问(线程安全)。 (继承自 Collection<T>。)
显式接口实现私有属性受 Silverlight for Windows Phone 支持ICollection.SyncRoot获取一个可用于同步对 ICollection 的访问的对象。 (继承自 Collection<T>。)
显式接口实现私有方法受 Silverlight for Windows Phone 支持IEnumerable.GetEnumerator返回一个循环访问集合的枚举数。 (继承自 Collection<T>。)
显式接口实现私有方法受 Silverlight for Windows Phone 支持IList.Add将某项添加到 IList 中。 (继承自 Collection<T>。)
显式接口实现私有方法受 Silverlight for Windows Phone 支持IList.Contains确定 IList 是否包含特定值。 (继承自 Collection<T>。)
显式接口实现私有方法受 Silverlight for Windows Phone 支持IList.IndexOf确定 IList 中特定项的索引。 (继承自 Collection<T>。)
显式接口实现私有方法受 Silverlight for Windows Phone 支持IList.Insert将某项插入 IList 中指定的索引处。 (继承自 Collection<T>。)
显式接口实现私有属性受 Silverlight for Windows Phone 支持IList.IsFixedSize获取一个值,该值指示 IList 是否具有固定大小。 (继承自 Collection<T>。)
显式接口实现私有属性受 Silverlight for Windows Phone 支持IList.IsReadOnly获取一个值,该值指示 IList 是否为只读。 (继承自 Collection<T>。)
显式接口实现私有属性受 Silverlight for Windows Phone 支持IList.Item获取或设置位于指定索引处的元素。 (继承自 Collection<T>。)
显式接口实现私有方法受 Silverlight for Windows Phone 支持IList.Remove IList 中移除特定对象的第一个匹配项。 (继承自 Collection<T>。)
显式接口实现私有事件INotifyPropertyChanged.PropertyChanged基础结构。在更改属性值时发生。
页首

 

备注
 
 

可以枚举实现 IEnumerable 接口的任何集合,这对于 Silverlight 中的一次性数据绑定已足够。 但是,若要设置动态绑定,以使集合中的插入或移除操作可以自动更新 UI,则该集合必须实现 INotifyCollectionChanged 接口。 此接口公开 CollectionChanged 事件,只要基础集合发生更改,都应该引发该事件。

Silverlight 提供 ObservableCollection<T> 类,此类是用以实现 INotifyCollectionChanged 接口以及 INotifyPropertyChanged 接口的基类数据集合。 它还具有预期的集合支持,这是通过从 Collection<T> 类派生来定义的。

有关示例,请参见如何绑定到分层数据并创建主/从视图

版本信息
 
 
Silverlight
受以下版本支持:5、4、3
Silverlight for Windows Phone
受以下版本支持:Windows Phone OS 7.1、Windows Phone OS 7.0
 
实例代码:姜彦 20180117
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tool.ParamConfig.ViewModel;
using Utility.Circuit.Controller;
using Utility.Circuit.Model;

namespace Tool.ParamConfig.View
{
    /// <summary>
    /// TFormulasView.xaml 的交互逻辑
    /// </summary>
    public partial class TFormulasView : UserControl
    {
        private ObservableCollection<TFormulaViewModel> FormulaVMs;
        public TFormulasView()
        {
            InitializeComponent();
            this.FormulaVMs = new ObservableCollection<TFormulaViewModel>();
            this.dgFormula.ItemsSource = this.FormulaVMs;
        }

        private void LoadFormula()
        {

            List<TFormula> Formulas = TFormulaController.GetFormulas();
            foreach (TFormula Formula in Formulas)
            {
                this.FormulaVMs.Add(new TFormulaViewModel(Formula));
            }

            int i = 0;
        }

        private void btnAddFormula_Click(object sender, RoutedEventArgs e)
        {
            TFormula Formula = TFormulaController.CreateFormula();
            if (TFormulaController.InsertFormula(Formula))
            {
                Formula.Id = TFormulaController.GetMaxID();
                this.FormulaVMs.Add(new TFormulaViewModel(Formula));
            }
            else
            {
                MessageBox.Show("添加规约失败!", "添加失败", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        private void btnDeleteFormula_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnSaveFormula_Click(object sender, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem((state) =>
            {
                foreach (TFormulaViewModel FormulaVM in this.FormulaVMs)
                {
                    if (FormulaVM.IsDataModied == true)
                    {
                        TFormulaController.UpdateFormula(FormulaVM.Model);
                        FormulaVM.IsDataModied = false;
                    }
                }
                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    MessageBox.Show("计算公式参数已成功保存至参数库中!", "计算保存成功", MessageBoxButton.OK, MessageBoxImage.Information);
                }));
            });
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadFormula();
        }

        //姜彦 20180112 21:23
        //string strPId1 = string.Empty;  //全局变量  为了传递cell的变化内容,上一个跟下一个对比的传递媒介
        //static BrushConverter conv1 = new BrushConverter();
        //Brush color0;//通过第三个值  交换颜色 原理 A B C 通过C 交换A B
        //Brush color1 = conv1.ConvertFromInvariantString("#E2E9E1") as Brush;//浅绿色EECEE0
        //Brush color2 = conv1.ConvertFromInvariantString("#FFFFFF") as Brush;//白色
        private void dgFormula_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            //var obj1 = e.Row.DataContext;
            //var vM = obj1 as TFormulaViewModel;
            //if (strPId1 == vM.StrId)
            //{
            //    e.Row.Background = (System.Windows.Media.Brush)color1;//浅绿色
            //}
            //else
            //{
            //    e.Row.Background = (System.Windows.Media.Brush)color2;//白色   
            //    color0 = color1;//通过第三个值  交换颜色 原理 A B C 通过C 交换A B
            //    color1 = color2;
            //    color2 = color0;
            //}
            //strPId1 = vM.StrId;   

            BrushConverter conv1 = new BrushConverter();
            Brush color1 = conv1.ConvertFromInvariantString("#E2E9E1") as Brush;//浅绿色EECEE0
            Brush color2 = conv1.ConvertFromInvariantString("#FFFFFF") as Brush;//白色

            var obj1 = e.Row.DataContext;
            var vM = obj1 as TFormulaViewModel;
            if (vM.ColorFlag)
            {
                e.Row.Background = (System.Windows.Media.Brush)color1;//浅绿色
            }
            else
            {
                e.Row.Background = (System.Windows.Media.Brush)color2;//白色   
            }



        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {


            //flowJumps = this.FlowJumps;
            ////按工序groupby flowjumps  
            //IEnumerable<IGrouping<int, FlowJump>> query =
            //flowJumps.GroupBy(pet => pet.processID, pet => pet);
            //foreach (IGrouping<int, FlowJump> info in query)
            //{
            //    List<FlowJump> sl = info.ToList<FlowJump>();//分组后的集合   
            //                                                //也可循环得到分组后,集合中的对象,你可以用info.Key去控制   
            //                                                //foreach (FlowJump set in info)   
            //                                                //{   
            //                                                //}   
            //}

            //List<TFormula> formulas = TFormulaController.GetFormulas();

            //IEnumerable<IGrouping<string, TFormula>> query =
            //    formulas.GroupBy(pet => pet.StrId, pet => pet);

            List<TFormula> formulas = TFormulaController.GetFormulas();

            //IEnumerable<IGrouping<string, List<TFormula>>> query =
            //    formulas.GroupBy(x => x.StrId).Select(x=>new List<IGrouping<string, List<TFormula>>> {});

            bool flag = true;

            var groupInfo = formulas.GroupBy(m => m.StrId).ToList();

            foreach (IGrouping<string,TFormula> info in groupInfo)
            {
                List<TFormula> s1 = info.ToList<TFormula>();

                foreach (TFormula f in s1)
                {
                    f.ColorFlag = flag;
                }

                flag = !flag;

                int j = 0;
            }


            int i = 0;

            //IEnumerable<IGrouping<string, List<TFormula>>> query =
            //    formulas.GroupBy(x => x.StrId).Select(x => new List<TFormula> { StrId = x.Key, Formulas = x.ToList() });

           // var groupTest = formulas.GroupBy(x => x.StrId).Select(x => new FormulaGroup { StrId = x.Key, Formulas = x.ToList() });


            TFormulaController.GetFormulaGroup();
        }
    }
}
View Code

 

 
 
 
 

 

转载于:https://www.cnblogs.com/jiangyan219/articles/8305182.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值