WPF MVVM 简单实现
来源:互联网 发布:网络小胖表情包 编辑:程序博客网 时间:2024/06/05 06:55
public class DelegateCommands:ICommand { public bool CanExecute(object parameter) { if (CanExecuteHander == null) return true; return CanExecuteHander(parameter); } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { if (ExecuteHander == null) return; ExecuteHander(parameter); } public Func<object, bool> CanExecuteHander; public Action<object> ExecuteHander; }
/// <summary> /// 通知属性 /// </summary> public class NotifyPropertyChanged:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void RasePropertyChange(string propertyName) { if (PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
public class AddViewModel:NotifyPropertyChanged { private int input1; public int Input1 { get { return this.input1; } set { this.input1 = value; this.RasePropertyChange("Input1"); } } private int input2; public int Input2 { get { return this.input2; } set { this.input2 = value; this.RasePropertyChange("Input2"); } } private int result; public int Result { get { return this.result; } set { this.result = value; this.RasePropertyChange("Result"); } } public DelegateCommands Add { get; set; } public void AddResult(object parameter) { this.Result = this.Input1 + this.Input2; } public AddViewModel() { this.Add = new DelegateCommands(); Add.ExecuteHander += new Action<object>(AddResult); } }
<StackPanel Orientation="Horizontal" Margin="20"> <TextBox Text="{Binding Input1}" ></TextBox> <TextBox Text="{Binding Input2}"></TextBox> <TextBox Text="{Binding Result}"></TextBox> <Button Command="{Binding Add}">加</Button> </StackPanel>
public MainWindow() { InitializeComponent(); this.DataContext = new AddViewModel(); }
注:在xaml文件中绑定数据以及命令的数据源,WPF默认情况下会使用冒泡的方式找DataContent的对象
0 0
- WPF MVVM 简单实现
- WPF---MVVM模式简单应用
- WPF-MVVM-ICommand接口实现
- MVVM简单实现
- 一个简单的WPF MVVM实例
- 一个简单的WPF MVVM实例
- WPF 一个MVVM的简单例子
- WPF 一个MVVM的简单例子
- WPF与MVVM的实现(一)MVVM简介
- 采用MVVM方式实现WPF的TreeView
- WPF+ListView+Linq+MVVM模式实现分页
- WPF mvvm模式下实现柱状图显示
- MVVM模式和在WPF中的实现
- WPF-MVVM
- wpf-mvvm
- wpf mvvm
- WPF MvvM
- WPF中使用MVVM创建一个简单的框架
- 安卓GridView异步加载网络图片
- c# XML和实体类之间相互转换(序列化和反序列化)
- POJ 2965 The Pilots Brothers' refrigerator
- 1、Struts框架学习
- Maven Project java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListene
- WPF MVVM 简单实现
- Linux kernel 内存子系统
- getTextBounds 方法作用
- android的Application使用
- 在内部存储中读写文件
- 【HUSTOJ】1065: 斜角II
- how to use dbx in Solaris -Debugging Your Program With dbx
- 天气JSON接口
- Linux下安装WPS的一些问题