I have one textbox and one button. The Button Command should change the property which is binded to the TextBox.
But I don’t see any visual changes after command execution.
I think it easy question related to wpf binding
Please help me with this issue
Source of app:
<UserControl.DataContext>
<local:SampleViewModel />
</UserControl.DataContext>
<Grid>
<StackPanel>
<TextBox Height="23" Width="120" Text="{Binding MyName}" />
<Button Content="Click" Command="{Binding ButtonCommand}" />
</StackPanel>
</Grid>
ViewModel:
Private _myName As String
Public Property MyName As String
Get
Return _myName
End Get
Set(value As String)
_myName = value
OnPropertyChanged("MyName")
End Set
End Property
Public _buttonCommand As DelegateCommand
Public ReadOnly Property ButtonCommand As DelegateCommand
Get
Return If(_buttonCommand IsNot Nothing, _buttonCommand,
New DelegateCommand(AddressOf Execute, AddressOf CanExecute))
End Get
End Property
Private Sub Execute()
MyName = "Executed"
End Sub
Private Function CanExecute() As Boolean
Return True
End Function
Public Event PropertyChanged As PropertyChangedEventHandler
Private Sub OnPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Do the following:
1.
Class MainWindow
Implements INotifyPropertyChanged
2 . In Public Sub New() make sure you write
Me.DataContext = Meto set the DataContextNote: Ignore step 2 if you’re using a ViewModel and setting it in XAML
3 . Modify the ProperyChanged like this:
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChangedOnly after implementing
INotifyPropertyChangedcorrectly will the Binding correctly refresh the MyName property after the PropertyChanged event