标签:
Command文件夹下的DeletgateCommand.cs
using System; using System.Windows.Input; namespace WPFMVVM1.Command { public class DeletgateCommand<T> : ICommand { private readonly Action<T> _executeMethod = null; private readonly Func<T, bool> _canExecuteMethod = null; public DeletgateCommand(Action<T> executeMethod) : this(executeMethod, null) { } public DeletgateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod) { if (executeMethod == null) throw new ArgumentNullException("executeMetnod"); _executeMethod = executeMethod; _canExecuteMethod = canExecuteMethod; } #region ICommand 成员 /// <summary> /// Method to determine if the command can be executed /// </summary> public bool CanExecute(T parameter) { if (_canExecuteMethod != null) { return _canExecuteMethod(parameter); } return true; } /// <summary> /// Execution of the command /// </summary> public void Execute(T parameter) { if (_executeMethod != null) { _executeMethod(parameter); } } #endregion event EventHandler ICommand.CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } #region ICommand 成员 public bool CanExecute(object parameter) { if (parameter == null && typeof(T).IsValueType) { return (_canExecuteMethod == null); } return CanExecute((T)parameter); } public void Execute(object parameter) { Execute((T)parameter); } #endregion } }
Model文件夹下的CauculatorModel.cs
namespace WPFMVVM1.Model { class CauculatorModel { public string FirstOperand { get; set; } public string SecondOperand { get; set; } public string Operation { get; set; } public string Result { get; set; } } }
View文件夹下的CalculatorView.xaml,注意设置为该窗口启动
<Window x:Class="WPFMVVM1.View.CalculatorView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WPFMVVM1.View" mc:Ignorable="d" Title="CalculatorView" Height="300" Width="300"> <Grid> <TextBox Height="23" Margin="12,63,0,0" Name="textBox1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" /> <Label Margin="12,25,95,0" Name="label2" Height="32" VerticalAlignment="Top">请输入x的值! x+x=?</Label> <Button Height="23" Command="{Binding AddCommand}" CommandParameter="{Binding ElementName=textBox1,Path=Text}" HorizontalAlignment="Left" Margin="12,102,0,0" Name="button1" VerticalAlignment="Top" Width="75"> 确定</Button> </Grid> </Window>
View文件夹下的CalculatorView.xaml.cs
using System.Windows; using WPFMVVM1.ViewModel; namespace WPFMVVM1.View { /// <summary> /// CalculatorView.xaml 的交互逻辑 /// </summary> public partial class CalculatorView : Window { public CalculatorView() { InitializeComponent(); this.DataContext = new CalculatorViewModel(); } } }
ViewModel文件夹下的CalculatorViewModel.cs
using System.Windows; using System.Windows.Input; using WPFMVVM1.Command; using WPFMVVM1.Model; namespace WPFMVVM1.ViewModel { public class CalculatorViewModel { CauculatorModel calculatorModel; private DeletgateCommand<string> addCommand; public CalculatorViewModel() { calculatorModel = new CauculatorModel(); } #region Public Properties public string FirstOperand { get { return calculatorModel.FirstOperand; } set { calculatorModel.FirstOperand = value; } } public string SecondOperand { get { return calculatorModel.SecondOperand; } set { calculatorModel.SecondOperand = value; } } public string Operation { get { return calculatorModel.Operation; } set { calculatorModel.Operation = value; } } public string Result { get { return calculatorModel.Result; } set { calculatorModel.Result = value; } } #endregion public ICommand AddCommand { get { if (addCommand == null) { addCommand = new DeletgateCommand<string>(Add, CanAdd); } return addCommand; } } public void Add(string x) { FirstOperand = x; SecondOperand = x; Result = (double.Parse(FirstOperand) + double.Parse(SecondOperand)).ToString(); Operation = "+"; MessageBox.Show(FirstOperand + Operation + SecondOperand + "=" + Result); } private static bool CanAdd(string num) { return true; } } }
ViewModel文件夹下的ViewModelBase.cs
using System.ComponentModel; namespace WPFMVVM1.ViewModel { public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } }
标签:
原文地址:http://www.cnblogs.com/guojiangze/p/5338140.html