ObservableCollection 类
Private Shared list As New CustomerList Public Shared Function GetList() As CustomerList
Return list
End Function
System.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
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()
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
参数 | 说明 |
---|---|
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)
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") 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 ...
随机推荐
- 线性代数之——微分方程和 exp(At)
本节的核心是将常系数微分方程转化为线性代数问题. \[\frac{du}{dt}=\lambda u \quad 的解为 \quad u(t) = Ce^{\lambda t}\] 代入 \(t=0\ ...
- [C++] in-class initializer
C++11 introduced serveral contructor-related enhancements including: Class member initializers Deleg ...
- wpa_supplicant与kernel交互
wpa_supplicant与kernel交互的操作,一般需要先明确驱动接口,以及用户态和kernel态的接口函数,以此来进行调用操作.这里分为4个步骤讨论. 1.首先需要明确指定的驱动接口.因为有较 ...
- Python中变量名里面的下划线
1 变量名前后都有两个下划线(__X__),表示是系统级变量: 2 变量名前只有一个下划线(_X),表示该变量不是由from module import *导入进来的: 3 变量名前有两个下划线(__ ...
- 《C++常见问题及解答》
一.类 1. 常数据成员的初始化只能在构造函数的初始化列表中进行 2. 静态数据成员不可以在类内初始化 3. 创建一个对象时的构造函数的调用次序:对象成员的构造函数.对象自身的构造函数 4. 创建一个 ...
- python异步初步窥探
1.异步之难:因为其执行吮吸不可预料,当下正要发生什么事件不可预料. 程序下一步行为往往依赖上一步值执行结果,如何知晓上次异步调用已完成并获取结果, 回调成了必然选择,那又 ...
- 【week2】 四则运算改进
四则运算满足简单加减乘除,以及包含括号的复杂四则运算. 代码描述: 1.采用random随机数产生要参与计算的数字,以及运算符号 2.采用Scanner获取控制台输入的结果,与计算出来的结果进行比对, ...
- NeoLoad系列- 快速上手教程
1.新建工程 2.点击录制脚本按钮 3.在弹出的开始录制对话框中,填写虚拟用户信息. Record in下拉框,用来填写用户路径,一般有三个容器组成: Init, Actions, and End.当 ...
- parse_str — 将字符串解析成多个变量
$arr2="first=value1&second=value2&third[]=value3&third[]=value4"; parse_str($a ...
- How To Disable MacBook ProTrackpad
How To Disable MacBook Pro Trackpad how to close macbook pro touchpad? https://www.wikihow.com/Chang ...