I wrote an extension method that helps with this.
Example Class using this extension method.
Public Interface INotifyPropertyChanged_Extd Inherits INotifyPropertyChanged Sub RaisePropertyChangedEvent(ByVal source As Object, e As PropertyChangedEventArgs) End Interface Public Interface INotifyPropertyChanging_Extd Inherits INotifyPropertyChanged_Extd Sub RaisePropertyChangingEvent(ByVal source As Object, e As PropertyChangingEventArgs) End Interface
Imports System.Runtime.CompilerServices Imports System.ComponentModel Imports System.Collections.Generic Public Module Exts <Extension> Public Sub SetProperty(Of T, U As INotifyPropertyChanging_Extd)(ByVal obj As U, [property] As String, toValue As T, ByRef usingField As T, Optional ignored As Object = Nothing) If obj Is Nothing Then Exit Sub If Not EqualityComparer(Of T).Default.Equals(usingField, toValue) Then Dim old = usingField obj.RaisePropertyChangingEvent(obj, New PropertyChangingEventArgs([property])) usingField = toValue obj.RaisePropertyChangedEvent(obj, New PropertyChangedEventArgs([property])) End If End Sub <Extension> Public Sub SetProperty(Of T, U As INotifyPropertyChanged_Extd)(ByVal obj As U, [property] As String, toValue As T, ByRef usingField As T) If obj Is Nothing Then Exit Sub If Not EqualityComparer(Of T).Default.Equals(usingField, toValue) Then usingField = toValue obj.RaisePropertyChangedEvent(obj, New PropertyChangedEventArgs([property])) End If End Sub End Module
Example Class using this extension method.
Public Class Example
Implements INotifyPropertyChanged_Extd
Private _Value As Integer
Property Value As Integer
Get
Return _Value
End Get
Set(value As Integer)
Me.SetProperty("Value", value, _Value)
End Set
End Property
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Public Sub RaisePropertyChangedEvent(source As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged_Extd.RaisePropertyChangedEvent
RaiseEvent PropertyChanged(source, e)
End Sub
End Class
Class MainWindow Private WithEvents MyExample As New Example Private Sub MyExample_PropertyChanged(sender As Object, e As ComponentModel.PropertyChangedEventArgs) Handles MyExample.PropertyChanged Debug.WriteLine(e.PropertyName) End Sub End Class
1 Comments On This Entry
Page 1 of 1
janne_panne
22 January 2012 - 09:06 AM
Nice snippet. For some reason it never came into my mind to create extension method for INotifyPropertyChanged interface so I'll be using this.
Page 1 of 1
|
|



1 Comments








|