ObservableCollection<T> 类

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

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

 

 

命名空间:  System.Collections.ObjectModel
程序集:  System.Windows(在 System.Windows.dll 中)
语法
 
 
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

 

 
 
 
 

 

posted @ 2018-01-17 19:49  <--青青子衿-->  阅读(268)  评论(0编辑  收藏  举报
// /**/ // 在页脚Html代码 引入 // function btn_donateClick() { var DivPopup = document.getElementById('Div_popup'); var DivMasklayer = document.getElementById('div_masklayer'); DivMasklayer.style.display = 'block'; DivPopup.style.display = 'block'; var h = Div_popup.clientHeight; with (Div_popup.style) { marginTop = -h / 2 + 'px'; } } function MasklayerClick() { var masklayer = document.getElementById('div_masklayer'); var divImg = document.getElementById("Div_popup"); masklayer.style.display = "none"; divImg.style.display = "none"; } setTimeout( function () { document.getElementById('div_masklayer').onclick = MasklayerClick; document.getElementById('btn_donate').onclick = btn_donateClick; var a_gzw = document.getElementById("guanzhuwo"); a_gzw.href = "javascript:void(0);"; $("#guanzhuwo").attr("onclick","follow('33513f9f-ba13-e011-ac81-842b2b196315');"); }, 900);