2012-11-30 154 views

回答

1

XAML:

<TextBox Text={Binding TextBoxAContent} /> 
<Button Command={Binding ButtonCommand} /> 

视图模型的代码应该是这样的:

class MainPageViewModel : ViewModelBase 
{ 
    private string _textBoxAContent; 
    public string TextBoxAContent 
    { 
     get {return _textBoxAContent;} 
     set { 
       _textBoxAContent = value; 
       RaisePropertyChanged("TextBoxAContent"); 
      } 
    } 

    public ICommand ButtonCommand 
    { 
     get 
     { 
      return new RelayCommand(ProcessTextHandler); 
     } 
    } 

    private void ProcessTextHandler() 
    { 
     //add your code here. You can process your textbox`s text using TextBoxAContent property. 
    } 
} 

你也应该分配您的视图模型通过视图控件的DataContext要查看的属性。 (简单地在构造函数)

public MainPage() 
{ 
    DataContext = new MainPageViewModel(); 
} 

UPD

P.S. RelayCommand & ViewModelBase - MVVM Light的类