我有一個文本框和一個按鈕。按鈕命令應該改變綁定到文本框的屬性。簡單的MVVM綁定問題
但是在執行命令後我沒有看到任何可見的變化。 我認爲簡單的問題涉及到WPF結合
請幫我解決這個問題
源應用程序:
<UserControl.DataContext>
<local:SampleViewModel />
</UserControl.DataContext>
<Grid>
<StackPanel>
<TextBox Height="23" Width="120" Text="{Binding MyName}" />
<Button Content="Click" Command="{Binding ButtonCommand}" />
</StackPanel>
</Grid>
視圖模型:
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
您是否嘗試在Execute()中放置斷點? – Blachshma
你可以嘗試在'Execute'處設置斷點並查看它是否被執行? – Vlad
不需要MyName是否可以觀察? – IronMan84