ObservableCollection 类 详解
Private Shared list As New CustomerListPublic Shared Function GetList() As CustomerListReturn list End Function
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
System.Collections.ObjectModel Imports System.ComponentModelPublic Class CustomerListInherits ObservableCollection(Of Customer)Private Shared list As New CustomerListPublic Shared Function GetList() As CustomerListReturn listEnd FunctionPrivate Sub New()' Make the constructor private, enforcing the "factory" concept ' the only way to create an instance of this class is by calling' the GetList method.AddItems()End SubPublic Shared Sub Reset()list.ClearItems()list.AddItems()End SubPrivate Sub AddItems()Add(New Customer("Maria Anders"))Add(New Customer("Ana Trujillo"))Add(New Customer("Antonio Moreno"))End Sub End Class
Private Sub AddItems()Add(New Customer("Maria Anders"))Add(New Customer("Ana Trujillo"))Add(New Customer("Antonio Moreno")) End Sub
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Imports System.ComponentModelPublic Class CustomerImplements INotifyPropertyChangedPublic Event PropertyChanged( _ByVal sender As Object, _ByVal e As PropertyChangedEventArgs) _Implements INotifyPropertyChanged.PropertyChangedProtected Overridable Sub OnPropertyChanged( _ByVal PropertyName As String)' Raise the event, and make this procedure' overridable, should someone want to inherit from' this class and override this behavior:RaiseEvent PropertyChanged( _Me, New PropertyChangedEventArgs(PropertyName))End SubPublic Sub New(ByVal Name As String)' Set the backing field so that you don't raise the ' PropertyChanged event when you first create the Customer._name = NameEnd SubPrivate _name As StringPublic Property Name() As StringGetReturn _nameEnd GetSet(ByVal value As String)If _name <> value Then_name = valueOnPropertyChanged("Name")End IfEnd SetEnd Property End Class
Public Event PropertyChanged( _ByVal sender As Object, _ByVal e As PropertyChangedEventArgs) _Implements INotifyPropertyChanged.PropertyChanged
Protected Overridable Sub OnPropertyChanged( _ByVal PropertyName As String)' Raise the event, and make this procedure' overridable, should someone want to inherit from' this class and override this behavior:RaiseEvent PropertyChanged( _Me, New PropertyChangedEventArgs(PropertyName)) End Sub
Private _name As StringPublic Property Name() As StringGetReturn _nameEnd GetSet(ByVal value As String)If _name <> value Then_name = valueOnPropertyChanged("Name")End IfEnd SetEnd Property
<ListBox DisplayMemberPath="Name"ItemsSource="{Binding ElementName=MainWindow, Path=Data}" Grid.Column="3" Grid.RowSpan="3" Name="ItemListBox" Margin="5" />
![](http://i.msdn.microsoft.com/dd252944.fig03%28zh-cn%29.gif)
Public WithEvents Data As CustomerList = CustomerList.GetList()
<Application x:Class="Application"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Startup="Application_Startup"><Application.Resources></Application.Resources> </Application>
Private Sub Application_Startup( _ByVal sender As System.Object, _ByVal e As System.Windows.StartupEventArgs)Dim window As New MainWindowwindow.Title = "Observable Collection 1"window.Show()window = New MainWindowwindow.Title = "Observable Collection 2"window.Show() End Sub
- 在 Visual Studio 2008 中,加载并运行示例应用程序。您会在同一窗口中看到两个实例。
- 单击以打开窗口左侧的组合框。请注意,控件包含 0、1 和 2 这三个数字,分别对应当前的三个用户。选择 1,会选中 Ana Trujillo 并将她的名字复制到文本框。
- 在一个窗口中,单击“删除”。Ana Trujillo 会从两个窗口中消失,因为两个 ListBox 控件都已绑定到同一 ObservableCollection 实例,而绑定使得更新会立即显示出来。再次打开组合框,请注意,现在仅会显示两个用户。在每个窗口中都尝试一下此操作,验证两个实例是否都是最新的。
- 再两次单击“删除”,删除所有用户。单击“重置数据”重新填充两个窗口中的列表。
- 在“添加新项”按钮旁边的文本框中,输入您自己的名字,然后单击“添加新项”。新名字即会显示在两个 ListBox 控件中。单击以打开组合框,并验证组合框现在是否包含 0 到 3 四个数字(每个数字分别对应一个用户)。验证两个窗口中的组合框都已更改,很明显,两个窗口的类都收到了指示集合已更改的事件。
- 在一个窗口的 ListBox 中,选择一个名字。在左侧较低的文本框中,修改名字并单击“更改”。首先,会出现一则警报,指示您已更改了属性,将其关闭后,您会立即看到两个窗口中的名字都已更改(请参见图 4)。
![](http://i.msdn.microsoft.com/dd252944.fig04%28zh-cn%29.gif)
Public WithEvents Data As CustomerList = CustomerList.GetList()
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Private Sub Data_CollectionChanged( _ByVal sender As Object, _ByVal e As NotifyCollectionChangedEventArgs) _Handles Data.CollectionChanged' Because the collection raises this event, you can modify your user ' interface on any window that displays controls bound to the data. On ' both windows, if you add or remove an item, all the controls update ' to indicate the new collection!' Did you add or remove an item in the collection?If e.Action = NotifyCollectionChangedAction.Add Or _e.Action = NotifyCollectionChangedAction.Remove Then' Set the list of integers in the combo box:SetComboDataSource()' Enable buttons as necessary:EnableButtons()End If End Sub
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
参数 | 说明 |
---|---|
Action | 检索引发事件的操作的相关信息。此属性包含 NotifyCollectionChangedAction 值,该值可以是 Add、Remove、Replace、Move 或 Reset。 |
NewItems | 检索更改集合时引入的新项目的列表。 |
NewStartingIndex | 检索发生更改的集合的索引。 |
OldItems | 检索受“替换”、“删除”或“移动”操作影响的旧项目列表。 |
OldStartingIndex | 检索执行了“替换”、“删除”或“移动”操作的集合的索引。 |
If e.Action = NotifyCollectionChangedAction.Add Or _e.Action = NotifyCollectionChangedAction.Remove Then
Private Sub SetComboDataSource()' Set the list of integers shown in the ' combo box:ItemComboBox.ItemsSource = _Enumerable.Range(0, Data.Count) End Sub
Private Sub HookupChangeEventHandler(ByVal cust As Customer)' Add a PropertyChanged event handler for ' the specified Customer instance:AddHandler cust.PropertyChanged, _AddressOf HandlePropertyChanged End Sub
Private Sub HookupChangeEventHandlers()For Each cust As Customer In DataHookupChangeEventHandler(cust)Next End Sub
' From DeleteItemButton_ClickDim index As Integer = ItemComboBox.SelectedIndex If index >= 0 ThenRemoveHandler Data.Item(index).PropertyChanged, _AddressOf HandlePropertyChangedData.RemoveAt(index)
' From NewItemButton_Clickcust = New Customer(NewItemTextBox.Text) HookupChangeEventHandler(cust) Data.Add(cust)
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Private Sub HandlePropertyChanged( _ ByVal sender As Object, _ ByVal e As PropertyChangedEventArgs)' In this particular application, you only want to bother with this ' code for the first window, although both will run the code. In this ' case, if the event was raised by the window whose title is ' "Observable Collection 1" then process the event:If Me.Title.EndsWith("1") ThenDim propName As String = e.PropertyNameDim myCustomer As Customer = CType(sender, Customer)' Unfortunately, no one hands you the old property value, or the new ' property value. You can use Reflection to retrieve the new property ' value, given the object that raised the event and the name of the ' property:Dim propInfo As System.Reflection.PropertyInfo = _GetType(Customer).GetProperty(propName)Dim value As Object = _propInfo.GetValue(myCustomer, Nothing)MessageBox.Show(String.Format( _"You changed the property '{0}' to '{1}'", _propName, value))End If End Sub
If Me.Title.EndsWith("1") Then'Code removed here… End If
Dim propName As String = e.PropertyName Dim myCustomer As Customer = CType(sender, Customer)
Dim propInfo As System.Reflection.PropertyInfo = _GetType(Customer).GetProperty(propName)
Dim value As Object = _propInfo.GetValue(myCustomer, Nothing)
转载于:https://www.cnblogs.com/llkey/archive/2013/05/30/3108608.html
ObservableCollection 类 详解相关推荐
- OpenCV Mat类详解和用法(官网原文)
参考文章:OpenCV Mat类详解和用法 我马克一下,日后更 官网原文链接:https://docs.opencv.org/3.2.0/d6/d6d/tutorial_mat_the_basic_i ...
- 转载:c+string类详解
C++ string 类详解 </h1><div class="clear"></div><div class="postBod ...
- JDBC学习笔记02【ResultSet类详解、JDBC登录案例练习、PreparedStatement类详解】
黑马程序员-JDBC文档(腾讯微云)JDBC笔记.pdf:https://share.weiyun.com/Kxy7LmRm JDBC学习笔记01[JDBC快速入门.JDBC各个类详解.JDBC之CR ...
- JDBC学习笔记01【JDBC快速入门、JDBC各个类详解、JDBC之CRUD练习】
黑马程序员-JDBC文档(腾讯微云)JDBC笔记.pdf:https://share.weiyun.com/Kxy7LmRm JDBC学习笔记01[JDBC快速入门.JDBC各个类详解.JDBC之CR ...
- Android复习14【高级编程:推荐网址、抠图片上的某一角下来、Bitmap引起的OOM问题、三个绘图工具类详解、画线条、Canvas API详解(平移、旋转、缩放、倾斜)、矩阵详解】
目 录 推荐网址 抠图片上的某一角下来 8.2.2 Bitmap引起的OOM问题 8.3.1 三个绘图工具类详解 画线条 8.3.16 Canvas API详解(Part 1) 1.transla ...
- Java中的Runtime类详解
Java中的Runtime类详解 1.类注释 /**Every Java application has a single instance of class Runtime that allows ...
- [NewLife.XCode]实体类详解
NewLife.XCode是一个有10多年历史的开源数据中间件,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示例代码和运行日志来进行深入分析,蕴含 ...
- basicdatasourcefactory mysql_Java基础-DBCP连接池(BasicDataSource类)详解
Java基础-DBCP连接池(BasicDataSource类)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 实际开发中"获得连接"或"释放资源 ...
- JAVA的StringBuffer类详解
JAVA的StringBuffer类详解 StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer ...
最新文章
- 基于OpenCV的行人目标检测
- SQL SERVER 使用 OPENRORWSET(BULK)函数将txt文件中的数据批量插入表中(2)
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
- 为选择合适的ERP供应商,是否该发布需求建议书(RFP)?
- 【剑指offer】——求出一个正整数的质数因子(Python)
- 一文梳理JavaScript中常见的七大继承方案
- Manacher入门
- Java NIO教程
- MS CRM 2011 如何创建基于SQL的自定义报表,并使用数据预筛选(Pre-Filtering)
- Atitit 使用js nodejs进行图像处理ocr的解决方案attilax总结
- 一文读懂: 什么是用户故事?What is User Stories?
- qmc0文件怎么转换mp3_怎么用手机把手机里的视频转换成mp3音乐?(手机,不是电脑)...
- html把字体设置为繁体,XP下怎样将繁体字设置成系统字体?XP下把系统字体改为繁体的方法...
- 手机屏幕物理点击器是什么原理_手机屏幕物理连点器
- 逆向教程-U3D游戏逆向分析(伊甸逆向分析)
- wd 文件服务器客服电话,wd 云服务器
- LFS : 制作分区和挂载分区
- bat批量安装软件,完成最后删除文件夹里所有安装包
- [导入]阿里妈妈广告牌生成器
- 解决QDialogButtonBox按钮的英文翻译问题