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 ...
随机推荐
- 清橙 A1318 加强版:Almost
题意: 直接看题面吧 原版:\(n \leq 1e5, q \leq 3e4, TL 5s, ML 256G\) 加强版1:\(n,q \leq 1.5e5, TL 5s, ML 256G\) 加强版 ...
- IMX6移植htop
top命令查看CPU利用率并不是很方便,因此打算移植htop到imx6上,主要包括以下几个步骤: - 资源下载 htop 下载http://hisham.hm/htop/releases/1.0.1/ ...
- Python高级编程-多进程
要让Python程序实现多进程(multiprocessing),我们先了解操作系统的相关知识. Unix/Linux操作系统提供了一个fork()系统调用,它非常特殊.普通的函数调用,调用一次,返回 ...
- ide的tomcat的部署和配置
关于intellij ide的tomcat的部署和配置 1.下载zip版的Tomcat 7,并解压.下载地址 2.在IDEA中配置Tomcat 7 在idea中的Settings(Ctrl+Alt ...
- BluetoothClass详解
一. BluetoothClass简介 1. 继承关系 public final class BluetoothClass extends Object implements Parcelable 该 ...
- WPF+数据库+三层
1.计算类 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...
- DataSet和List 泛型之间互相转换 (转载)
//DataSet与泛型集合间的互相转换 //利用反射机制将DataTable的字段与自定义类型的公开属性互相赋值. //注意:从DataSet到IList<T>的转换,自定义类型的公开属 ...
- 【Mysql】- Mysql 8.0正式版新亮点
MySQL 8.0 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 注意:从 MySQL 5.7 升级到 MySQL 8 ...
- 《Effective C#》快速笔记(四)- 使用框架
.NET 是一个类库,你了解的越多,自己需要编写的代码就越少. 目录 三十.使用重写而不是事件处理函数 三十一.使用 IComparable<T> 和 IComparer<T> ...
- ExtremeComponents源码解析(一)
一.前言 因参与公司框架改造,在负责前端table组件选型时,原本选了jqGrid和Bootstraptable作为备选方案,评审会上,武哥提了EXtremeComponents,让我也去了解下,看下 ...