Skip to content

Instantly share code, notes, and snippets.

@okayamadaiti
Created May 7, 2020 11:51
Show Gist options
  • Save okayamadaiti/9a08f48a73997466c1a18d52b73fb44c to your computer and use it in GitHub Desktop.
Save okayamadaiti/9a08f48a73997466c1a18d52b73fb44c to your computer and use it in GitHub Desktop.
Visual Basic でのBindableBase
Public MustInherit Class BindableBase : Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Function SetProperty(Of t)(ByRef storage As t, value As t, Optional propertyName As String = Nothing) As Boolean
If Object.Equals(storage, value) Then Return False
storage = value
Me.OnPropertyChanged(propertyName)
Return True
End Function
Protected Sub OnPropertyChanged(Optional propertyName = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
DelegateCommand
Public Class DelegateCommand : Implements ICommand
Private _execute As Action(Of Object)
Private _canExecute As Func(Of Object, Boolean)
Public Sub New(execute As Action(Of Object))
Me._execute = execute
Me._canExecute = Nothing
End Sub
Public Sub New(execute As Action(Of Object), canExecute As Func(Of Object, Boolean))
Me._execute = execute
Me._canExecute = canExecute
End Sub
Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
Public Sub RaiseCanExecuteChanged()
RaiseEvent CanExecuteChanged(Me, EventArgs.Empty)
End Sub
Public Sub Execute(parameter As Object) Implements ICommand.Execute
If (Me._execute IsNot Nothing) Then Me._execute(parameter)
End Sub
Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
Return If(Me._canExecute IsNot Nothing, Me._canExecute(parameter), True)
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment