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 ...
随机推荐
- [堆+贪心]牛客练习赛40-B
传送门:牛客练习赛40 题面: 小A手头有 n 份任务,他可以以任意顺序完成这些任务,只有完成当前的任务后,他才能做下一个任务 第 i 个任务需要花费 x_i 的时间,同时完成第 i 个任务的时间不 ...
- QT打开文件路径中含有中文和空格问题
使用qt-mingw版做的软件,发给客户以后说工作不正常,配置文件无法打开,或者加载数据文件不正常.远程查看以后,发现客户经常将程序放置在中文带空格的路径下,导致文件打开不正常.所以最近想在程序上解决 ...
- Windows Phone编程回顾
前言 已有一年多没有碰WP相关的开发了. 近期经常看博客园的文章, 发现开发WP应用的同学很多, 其中博问频道关于"WPF", "C#", "WP8& ...
- Alpha冲刺——第四天
Alpha第四天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- 项目--uml
[团队信息] 团队项目: 小葵日记--主打记录与分享模式的日记app 队名:日不落战队 队员信息及贡献分比例: 短学号 名 本次作业博客链接 此次作业任务 贡献分配 备注 501 安琪 http:// ...
- lintcode-189-丢失的第一个正整数
189-丢失的第一个正整数 给出一个无序的正数数组,找出其中没有出现的最小正整数. 样例 如果给出 [1,2,0], return 3 如果给出 [3,4,-1,1], return 2 挑战 只允许 ...
- lintcode-15-全排列
全排列 给定一个数字列表,返回其所有可能的排列. 注意事项 你可以假设没有重复数字. 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3], [2,3 ...
- js图片转换为base64
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【Linux】- Ubuntu 配置mysql远程访问
ubuntu上安装mysql非常简单只需要几条命令就可以完成. sudo apt-get install mysql-server 安装过程中会提示设置密码什么的,注意设置了不要忘了,安装完成之后 ...
- IE8 兼容CSS3 使用 PIE.htc
在需要的标签中 div { border:; border-bottom: 10px solid transparent; border-image: url(../images/border-img ...