WPF中MVVM方式绑定命令

来源:互联网 时间:1970-01-01

在页面中绑定数据源,绑定显示的内容,绑定按钮的命令,当点击按钮的时候,绑定了{Binding Analysis}的空间内容将自动更新。

<TextBlock Text="{Binding Analysis}" /><Button Content="影响分析" Height="23" Command="{Binding AnalysisCommand}" Width="75" />

ViewModel中的绑定到前台页面的命令和内容。

 private string _analysis = string.Empty; public string Analysis { get { return this._analysis; } set { this._analysis = value; Changed("Analysis"); } } public ICommand AnalysisCommand { get { return new AnalysisCommand(this); } } public void AnalysisResult() { Analysis = "以上的解决方案在执行过程中可能发生问题!"; }

命令绑定方式。

 class AnalysisCommand:ICommand { private AddEmergencyViewModel _AddEmergencyViewModel; public bool CanExecute(object parameter) { return true; }  public event EventHandler CanExecuteChanged { add { } remove { } } public AnalysisCommand(AddEmergencyViewModel addEmergencyViewModel) { this._AddEmergencyViewModel = addEmergencyViewModel; } public void Execute(object parameter) { this._AddEmergencyViewModel.AnalysisResult(); } }

原文地址:http://www.luacloud.com/2011/05/17/wpf%e4%b8%admvvm%e6%96%b9%e5%bc%8f%e7%bb%91%e5%ae%9a%e5%91%bd%e4%bb%a4/

相关阅读:
Top