https://msdn.microsoft.com/zh-cn/library/ms668604(VS.95).aspx
表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。
System.Object
System.Collections.ObjectModel.Collection<T>
System.Collections.ObjectModel.ObservableCollection<T>
Microsoft.Internal.Pivot.Interactivity.AttachedCollection<TAttachable>
System.Data.Services.Client.DataServiceCollection<T>
System.Windows.Controls.CalendarBlackoutDatesCollection
System.Windows.Controls.Pivot.PivotViewerItemTemplateCollection
System.Windows.Controls.SelectedDatesCollection
System.Collections.ObjectModel.Collection<T>
System.Collections.ObjectModel.ObservableCollection<T>
Microsoft.Internal.Pivot.Interactivity.AttachedCollection<TAttachable>
System.Data.Services.Client.DataServiceCollection<T>
System.Windows.Controls.CalendarBlackoutDatesCollection
System.Windows.Controls.Pivot.PivotViewerItemTemplateCollection
System.Windows.Controls.SelectedDatesCollection
命名空间: System.Collections.ObjectModel
程序集: System.Windows(在 System.Windows.dll 中)
程序集: System.Windows(在 System.Windows.dll 中)
ObservableCollection<T> 类型公开以下成员。
显示: 继承 保护
名称 | 说明 | |
---|---|---|
ObservableCollection<T>() | 初始化 ObservableCollection<T> 类的新的空实例。 | |
ObservableCollection<T>(IEnumerable<T>) | 初始化 ObservableCollection<T> 类的新实例,并用从指定集合中复制的项来填充该实例。 | |
ObservableCollection<T>(List<T>) | 初始化 ObservableCollection<T> 类的新实例,并用从指定列表中复制的项来填充该实例。
|
显示: 继承 保护
名称 | 说明 | |
---|---|---|
Count | 获取 Collection<T> 中实际包含的元素数。 (继承自 Collection<T>。) | |
Item | 获取或设置位于指定索引处的元素。 (继承自 Collection<T>。) | |
Items | 获取 Collection<T> 周围的 IList<T> 包装。 (继承自 Collection<T>。) |
显示: 继承 保护
显示: 继承 保护
名称 | 说明 | |
---|---|---|
ICollection.CopyTo | 从特定的 Array 索引处开始,将 ICollection 的元素复制到一个 Array 中。 (继承自 Collection<T>。) | |
ICollection<T>.IsReadOnly | 获取一个值,该值指示 ICollection<T> 是否为只读。 (继承自 Collection<T>。) | |
ICollection.IsSynchronized | 获取一个值,该值指示是否同步对 ICollection 的访问(线程安全)。 (继承自 Collection<T>。) | |
ICollection.SyncRoot | 获取一个可用于同步对 ICollection 的访问的对象。 (继承自 Collection<T>。) | |
IEnumerable.GetEnumerator | 返回一个循环访问集合的枚举数。 (继承自 Collection<T>。) | |
IList.Add | 将某项添加到 IList 中。 (继承自 Collection<T>。) | |
IList.Contains | 确定 IList 是否包含特定值。 (继承自 Collection<T>。) | |
IList.IndexOf | 确定 IList 中特定项的索引。 (继承自 Collection<T>。) | |
IList.Insert | 将某项插入 IList 中指定的索引处。 (继承自 Collection<T>。) | |
IList.IsFixedSize | 获取一个值,该值指示 IList 是否具有固定大小。 (继承自 Collection<T>。) | |
IList.IsReadOnly | 获取一个值,该值指示 IList 是否为只读。 (继承自 Collection<T>。) | |
IList.Item | 获取或设置位于指定索引处的元素。 (继承自 Collection<T>。) | |
IList.Remove | 从 IList 中移除特定对象的第一个匹配项。 (继承自 Collection<T>。) | |
INotifyPropertyChanged.PropertyChanged | 基础结构。在更改属性值时发生。 |
可以枚举实现 IEnumerable 接口的任何集合,这对于 Silverlight 中的一次性数据绑定已足够。 但是,若要设置动态绑定,以使集合中的插入或移除操作可以自动更新 UI,则该集合必须实现 INotifyCollectionChanged 接口。 此接口公开 CollectionChanged 事件,只要基础集合发生更改,都应该引发该事件。
Silverlight 提供 ObservableCollection<T> 类,此类是用以实现 INotifyCollectionChanged 接口以及 INotifyPropertyChanged 接口的基类数据集合。 它还具有预期的集合支持,这是通过从 Collection<T> 类派生来定义的。
有关示例,请参见如何绑定到分层数据并创建主/从视图。
Silverlight
受以下版本支持:5、4、3Silverlight 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(); } } }