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 ...
随机推荐
- 七:HDFS Permissions Guide 权限
1.权限模式 简单:启动HDFS的操作系统用户即为超级用户,可以通过HADOOP_USER_NAME指定 kerberos: 2.group mapping 组列表由grou ...
- Linux 静态和动态添加路由
(一)静态添加 1/5 首先让我们查看当前机器的路由表,执行如下命令:route -n [root@vnode33 network-scripts]# route -n Kernel IP routi ...
- POJ 2069 Super Star(计算几何の最小球包含+模拟退火)
Description During a voyage of the starship Hakodate-maru (see Problem 1406), researchers found stra ...
- MFC常用数据类型
下面这些是和Win32程序共同使用的数据类型BOOL:布尔值,取值为TRUE or FALSEBSTR:32-bit 字符指针BYTE:8-bit整数,未带正负号COLORREF:32-bit数值,代 ...
- DAY2敏捷冲刺
站立式会议 工作安排 (1)服务器配置 (2)数据库连接 (3)页面创意 燃尽图 代码提交记录 感想 林一心:centos配置服务器真的算是一个不小的坑,目前数据库配置清楚,脚本部署好明天测试交互,还 ...
- 第三章——供机器读取的数据(XML)
本书使用的文件.代码:https://github.com/huangtao36/data_wrangling 机器可读(machine readable)文件格式: 1.逗号分隔值(Comma-Se ...
- Node js路由
/* 要为程序提供请求的 URL 和其他需要的 GET 及 POST 参数,随后程序需要根据这些数据来执行相应的代码. 因此,需要查看 HTTP 请求,从中提取出请求的 URL 以及 GET/POST ...
- LoadRunner数据库监控指标
SQL Server 注:以下指标取自SQL Server自身提供的性能计数器. 指标名称 指标描述 指标范围 指标单位 1.SQL Server中访问方法(Access Methods)对象包含的性 ...
- php中的<?= ?>替换<?php echo ?>
首先修改PHP.ini文件.如下: 1. 将short_open_tag = Off 改成On 开启以后可以使用PHP的短标签:<? ?> <?= $test ?>来代替 &l ...
- 错误 10 非静态的字段、方法或属性“Test10.Program.a”要求对象引用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test ...