ObservableCollection 类
Private Shared list As New CustomerList Public Shared Function GetList() As CustomerList
Return list
End Function
  图 1 CustomerListSystem.Collections.ObjectModel
Imports System.ComponentModel Public Class CustomerList
Inherits ObservableCollection(Of Customer) Private Shared list As New CustomerList Public Shared Function GetList() As CustomerList
Return list
End Function Private 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 Sub Public Shared Sub Reset()
list.ClearItems()
list.AddItems()
End Sub Private 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
  图 2 具有 PropertyChanged 事件的 Customer 类Imports System.ComponentModel Public Class Customer
Implements INotifyPropertyChanged 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 Public 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 = Name
End Sub Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
If _name <> value Then
_name = value
OnPropertyChanged("Name")
End If
End Set
End 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 String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
If _name <> value Then
_name = value
OnPropertyChanged("Name")
End If
End Set
End Property
<ListBox
DisplayMemberPath="Name"
ItemsSource=
"{Binding ElementName=MainWindow, Path=Data}"
Grid.Column="3" Grid.RowSpan="3" Name="ItemListBox"
Margin="5" />
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 MainWindow
window.Title = "Observable Collection 1"
window.Show() window = New MainWindow
window.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)。
 
Public WithEvents Data As CustomerList = CustomerList.GetList()
  图 5 检查更改的集合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
  图 6 NotifyCollectionChangedEventArgs 参数| 参数 | 说明 | 
|---|---|
| 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 Data
HookupChangeEventHandler(cust)
Next
End Sub
' From DeleteItemButton_Click Dim index As Integer = ItemComboBox.SelectedIndex
If index >= 0 Then
RemoveHandler Data.Item(index).PropertyChanged, _
AddressOf HandlePropertyChanged Data.RemoveAt(index)
' From NewItemButton_Click cust = New Customer(NewItemTextBox.Text)
HookupChangeEventHandler(cust)
Data.Add(cust)
  图 7 使用 Reflection 的 HandlePropertyChangedPrivate 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") Then
Dim propName As String = e.PropertyName
Dim 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)
ObservableCollection 类的更多相关文章
- ObservableCollection类
		
https://blog.csdn.net/GongchuangSu/article/details/48832721 https://blog.csdn.net/hyman_c/article/de ...
 - ObservableCollection
		
1)可以使绑定控件与基础数据源保持同步2)还可以在您添加.删除.移动.刷新或替换集合中的项目时引发 CollectionChanged 事件3)还可以在您的窗口以外的代码修改基础数据时做出反应4)相互 ...
 - 属性更改通知(INotifyPropertyChanged)——针对ObservableCollection
		
问题 在开发webform中,wpf中的ObservableCollection<T>,MSDN中说,在添加项,移除项时此集合通知控件,我们知道对一个集合的操作是CURD但是恰恰没有Upd ...
 - Java类的继承与多态特性-入门笔记
		
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
 - WPF 数据绑定Binding
		
什么是数据绑定? Windows Presentation Foundation (WPF) 数据绑定为应用程序提供了一种简单而一致的方法来显示数据以及与数据交互. 通过数据绑定,您可以对两个不同对象 ...
 - silverlight简单数据绑定3
		
3种数据绑定模式 OneTime(一次绑定) OneWay(单项绑定) TwoWay(双向绑定) OneTime:仅在数据绑定创建时使用数据源更新目标. 列子: 第一步,创建数据源对象让Person ...
 - 调用Ria Service中方法的各种方式
		
前端界面后台: using System; using System.Collections.Generic; using System.Linq; using System.Net; using S ...
 - wp7学习笔记
		
1.xap:最终是压缩包:最终部署有系统控制,防止流亡软件:放到固有位置productid;有的文件放在.dll中或直接放入目录下:控制有生成操作:content,内容,content效率更高不用从. ...
 - WPF学习之数据绑定
		
WPF中的数据绑定提供了很强大的功能.与普通的WinForm程序相比,其绑定功能为我们提供了很多便利,例如Binding对象的自动通知/刷新,Converter,Validation Rules,Tw ...
 
随机推荐
- Jedis 与 MySQL的连接线程安全问题
			
Jedis的连接是非线程安全的 下面是set命令的执行过程,简单分为两个过程,客户端向服务端发送数据,服务端向客户端返回数据,从下面的代码来看:从建立连接到执行命令是没有进行任何并发同步的控制 pub ...
 - [知识库:python-tornado]异步调用中的上下文控制Tornado stack context
			
异步调用中的上下文控制Tornado stack context https://www.zouyesheng.com/context-in-async-env.html 这篇文章真心不错, 非常透彻 ...
 - 《剑指Offer》题六十一~题六十八
			
六十一.扑克牌中的顺子 题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的.2~10为数字本身,A为1,J为11,Q为12,K为13,而大.小王可以看成任意数字. 六十二.圆圈中 ...
 - oracle数据库之存储函数和过程
			
一.引言 ORACLE 提供可以把 PL/SQL 程序存储在数据库中,并可以在任何地方来运行它.这样就叫存储过程或函数.过程和函数统称为 PL/SQL 子程序,他们是被命名的 PL/SQL 块 ...
 - lintcode-144-交错正负数
			
144-交错正负数 给出一个含有正整数和负整数的数组,重新排列成一个正负数交错的数组. 注意事项 不需要保持正整数或者负整数原来的顺序. 样例 给出数组[-1, -2, -3, 4, 5, 6],重新 ...
 - Linux防火墙iptables学习
			
http://blog.chinaunix.net/uid-9950859-id-98277.html 要在网上传输的数据会被分成许多小的数据包,我们一旦接通了网络,会有很多数据包进入,离开,或者经过 ...
 - windows批处理学习(call与start)---02
			
参考:https://www.cnblogs.com/Braveliu/p/5078283.html 一.call命令总结 (1)call命令简介 语法: call [ [Drive:] [Path] ...
 - VS2010中的sln,suo分别是什么含义
			
我们通过双击.sln加载出我们的工程. Visual Studio.NET采用两种文件类型(.sln和.suo)来存储特定于解决方案的设置,它们总称为解决方案文件.为解决方案资源管理器提供显示管理文件 ...
 - 第43天:事件对象event
			
一.事件对象事件:onmouseover. onmouseout. onclickevent //事件的对象 兼容写法:var event = event || window.event; event ...
 - html5 canvas 图像处理
			
1.图像放大缩小 <script> var cvs = document.getElementById("canvas"); cvs.width = cvs.heigh ...