当用户单击按钮时,我有一个带有文本框和按钮的 Xaml 页面文本框值应该传递给它的 viewModel。如何做到这一点?
问问题
95 次
1 回答
1
xml:
<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
ps RelayCommand & ViewModelBase - 来自MVVM Light的类
于 2012-11-30T07:13:16.903 回答