MVVM示例
来源:互联网 发布:淘宝怎么看不到v等级了 编辑:程序博客网 时间:2024/05/21 02:52
//DelegateCommand(命令绑定类,实现:ICommand接口)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WpfMVVM.Commands
{
class DelegateCommand:ICommand
{
public bool CanExecute(object parameter)
{
if (this.CanExecuteFunc==null)
{
return true;
}
return this.CanExecuteFunc(parameter);
}
public void Execute(object parameter)
{
if (this.ExecuteAction==null)
{
return;
}
this.ExecuteAction(parameter);
}
public Func<object,bool> CanExecuteFunc { get; set; }//Func是一个委托,第一个类型是参数类型,第二个类型是返回值类型
public Action<object> ExecuteAction { get; set; }//Acton也是一个委托,但他没有返回值,里面类型是参数的类型
public event EventHandler CanExecuteChanged;
}
}
//ViewModel基类(实现INotifyPropertyChanged接口,VeiwModel类继承该类,也可以ViewModel直接是实现该接口也可以)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace WpfMVVM.ViewModels
{
class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
//ViewModel类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using WpfMVVM.Commands;
namespace WpfMVVM.ViewModels
{
class ViewModelMainClass : NotificationObject
{
private double input1;
public double Input1
{
get { return input1; }
set
{
input1 = value;
this.RaisePropertyChanged("Input1");
}
}
private double input2;
public double Input2
{
get { return input2; }
set
{
input2 = value;
this.RaisePropertyChanged("Input2");
}
}
private double result;
public double Result
{
get { return result; }
set
{
result = value;
this.RaisePropertyChanged("Result");
}
}
public DelegateCommand AddCommand { get; set; }
private void Add(object parameter)
{
this.Result = this.Input1 + this.Input2;
}
public ViewModelMainClass()
{
this.AddCommand = new DelegateCommand();
this.AddCommand.ExecuteAction=new Action<object>(this.Add);
}
}
}
//xaml代码
<Grid>
<TextBox Text="{Binding Input1}" x:Name="tb1" Height="60" Background="LemonChiffon" Margin="10,0,10,240"/>
<TextBox Text="{Binding Input2}" x:Name="tb2" Height="60" Background="LemonChiffon" Margin="10,70,10,180" />
<TextBox Text="{Binding Result}" x:Name="tb3" Height="60" Background="LemonChiffon" Margin="10,140,10,120"/>
<Button x:Name="bt1" Command="{Binding AddCommand}" Margin="0,240,0,0">Add</Button>
</Grid>
- MVVM示例
- MVVM示例
- MVVM示例讲解
- MVVM示例讲解
- MVVM教程(二):开始MVVM示例
- 【WPF】【DevExpress】TileLayoutControl+MVVM示例
- WPF经典编程模式-MVVM示例讲解
- WPF经典编程模式-MVVM示例讲解
- WPF经典编程模式-MVVM示例讲解
- WPF经典编程模式-MVVM示例讲解
- WPF经典编程模式-MVVM示例讲解
- MVVM
- MVVM
- MVVM
- MVVM
- MVVM
- MVVM
- MVVM
- 接口测试的感悟
- 网页打开自己加载到指定位置
- IOS程序国际化
- [二分]幂次方
- 在Java里处理文件的技巧
- MVVM示例
- windows7环境下为eclipse安装CDT插件
- UVA-10641 Barisal Stadium (DP+几何)
- Json在java中的使用
- 'VBoxManage' is not recognized as an internal or external command, operable program or batch file.
- 分析JVM gc日志
- Linux route命令的使用详解
- 关于Web api授权的问题
- [剑指offer]二叉搜索树与双向链表