vb.net 中的变量/属性更改事件 [英] variable/property changed event in vb.net

查看:25
本文介绍了vb.net 中的变量/属性更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建在更改变量或属性时引发的事件(或者我是否可以将要放在事件中的代码放在属性的 Set 部分?

How can I make an event that is raised when a variable or property is changed (or can I just put the code I would put in an event in the Set section of a property?

推荐答案

来自 MSDN 库条目 INotifyPropertyChanged.PropertyChanged 事件:

Public Class DemoCustomer
    Implements INotifyPropertyChanged

    Private customerNameValue As String = String.Empty

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Public Property CustomerName() As String
        Get
            Return Me.customerNameValue
        End Get

        Set(ByVal value As String)
            If Not (value = customerNameValue) Then
                Me.customerNameValue = value
                NotifyPropertyChanged("CustomerName")
            End If
        End Set
    End Property
End Class

这篇关于vb.net 中的变量/属性更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆