2012-10-16 102 views
1

我有一個文本框和一個按鈕。按鈕命令應該改變綁定到文本框的屬性。簡單的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 
+0

您是否嘗試在Execute()中放置斷點? – Blachshma

+0

你可以嘗試在'Execute'處設置斷點並查看它是否被執行? – Vlad

+0

不需要MyName是否可以觀察? – IronMan84

回答

1

執行如下:

1. 類主窗口 實現INotifyPropertyChanged

2。在公共的Sub New(),請確保你寫Me.DataContext = Me設置的DataContext

注意:如果您使用的是視圖模型和XAML

3設置它忽略第2步。修改ProperyChanged這樣的: Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

只有implementing INotifyPropertyChanged correctly之後將正確綁定刷新PropertyChanged事件

+0

Downvoter謹慎解釋? – Blachshma

+0

他的代碼不起作用,我能夠在VS 2010上重現他的問題。以下解決了我的計算機上的問題。 – Blachshma

+0

我無法用他的VB和XAML(VS12)重現他的問題。我想知道他是否從類聲明中省略了「Implements INotifyPropertyChanged」。這是我認爲他做的代碼中唯一的部分。我在假設OP省略了INPC頭部代碼的情況下刪除了我的downvote。 – user7116

1

這裏後MYNAME屬性是與您的具體XAML工作的代碼(我把DelegateCommand實現從http://wpftutorial.net/DelegateCommand.html)對不起,它是C#,我真的沒有進入VB:D

public class DelegateCommand : ICommand 
{ 
    private readonly Predicate<object> _canExecute; 
    private readonly Action<object> _execute; 

    public event EventHandler CanExecuteChanged; 

    public DelegateCommand(Action<object> execute) 
     : this(execute, null) 
    { 
    } 

    public DelegateCommand(Action<object> execute, 
        Predicate<object> canExecute) 
    { 
     _execute = execute; 
     _canExecute = canExecute; 
    } 

    public virtual bool CanExecute(object parameter) 
    { 
     if (_canExecute == null) 
     { 
      return true; 
     } 

     return _canExecute(parameter); 
    } 

    public virtual void Execute(object parameter) 
    { 
     _execute(parameter); 
    } 

    public void RaiseCanExecuteChanged() 
    { 
     if (CanExecuteChanged != null) 
     { 
      CanExecuteChanged(this, EventArgs.Empty); 
     } 
    } 
} 

public class SampleViewModel : INotifyPropertyChanged 
{ 
    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 

    public DelegateCommand _buttonCommand; 
    public DelegateCommand ButtonCommand 
    { 
     get 
     { 
      if (_buttonCommand == null) 
      { 
       _buttonCommand = new DelegateCommand(Execute); 
      } 
      return _buttonCommand; 
     } 
    } 

    public void Execute(object o) 
    { 
     MyName = "executed"; 
    } 

    public string MyName { get { return _myName; } set { _myName = value; OnPropertyChanged("MyName"); } } 

    private string _myName; 
}