一、效果图
通过+10,-10,0按钮,对文本框数据进行修改。
二、代码
Model层
myModel.cs
class myModel
{
private int num=0;
public int Num
{
get { return num; }
set
{
num = value;
}
}
}
ViewModel层
1.ViewModelBase.cs
using System.ComponentModel; //添加命名空间
namespace myWpf_command.ViewModel
{ //继承INotifyPropertyChanged接口
class ViewModelBase:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyname)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
}
}
- Delegatecommand.cs
using System.Windows.Input;
namespace myWpf_command.ViewModel
{
class Delegatecommand:ICommand
{
//定义两个个委托
private Action<object> executeCommand;
public Action<object> ExecuteCommand
{
get { return executeCommand; }
set { executeCommand = value; }
}
private Func<object, bool> canExecuteCommand;
public Func<object, bool> CanExecuteCommand
{
get { return canExecuteCommand; }
set { canExecuteCommand = value; }
}
//有参数的构造函数
public Delegatecommand(Action<object> excuteCommand, Func<object, bool> canExcuteCommand)
{
this.executeCommand = excuteCommand;
this.canExecuteCommand = canExcuteCommand;
}
public bool CanExecute(object parameter)
{
if (CanExecuteCommand != null)
{
return this.CanExecuteCommand(parameter);
}
else
{
return true;
}
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (this.ExecuteCommand != null)
{
this.ExecuteCommand(parameter);
}
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteCommand != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}
3.CaculatorViewModelcs.cs
using System.Windows.Input;
namespace myWpf_command.ViewModel
{
class CaculatorViewModelcs:ViewModelBase
{
private int num1;
public int Num1
{
get { return num1; }
set
{
num1 = value;
this.RaisePropertyChanged("Num1");
}
}
//基本方法
public void Add(object param)
{
Num1 = Num1 + 10;
}
public void Reduce(object param)
{
Num1 = Num1 - 10;
}
public void Rest(object param)
{
Num1 =0;
}
//命令
public ICommand AddCommand { get; set; }
public ICommand ReduceCommand { get; set; }
public ICommand ResetCommand { get; set; }
//构造函数
public CaculatorViewModelcs()
{
// Num1 = mymodel.Num;
var model = new Model.myModel();
num1 = model.Num;
AddCommand = new Delegatecommand(Add, null);
ReduceCommand = new Delegatecommand(Reduce, null);
ResetCommand = new Delegatecommand(Rest, null);
}
}
}
View层
MainWindow.xaml
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="148*"/>
<ColumnDefinition Width="185*"/>
<ColumnDefinition Width="184*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="81*"/>
<RowDefinition Height="108*"/>
<RowDefinition Height="131*"/>
</Grid.RowDefinitions>
<Button Content="+10" HorizontalAlignment="Left" Margin="63,58,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Height="22" Command="{Binding AddCommand}"/>
<Button Content="-10" Grid.Column="2" HorizontalAlignment="Left" Margin="46,58,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Command="{Binding ReduceCommand}" />
<Button Content="0" Grid.Column="1" HorizontalAlignment="Left" Margin="61,58,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Command="{Binding ResetCommand}"/>
<TextBox Grid.Column="1" HorizontalAlignment="Left" Height="29" Margin="0,40,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Num1}" VerticalAlignment="Top" Width="185" IsReadOnly="True"/>
<Label Content="WPF MVVM 练习" Grid.Column="1" HorizontalAlignment="Left" Margin="36,45,0,0" VerticalAlignment="Top"/>
</Grid>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel.CaculatorViewModelcs();
}
}
新手学习,还请各路大神多多指导~~~~~