1.创建view界面
<Window x:Class="WpfFramework.MainWindow"
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:WpfFramework"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Label Content="参数" HorizontalAlignment="Left" Margin="210,123,0,0" VerticalAlignment="Top"/>
<Label Content="密码" HorizontalAlignment="Left" Margin="210,93,0,0" VerticalAlignment="Top"/>
<Label Content="账号" HorizontalAlignment="Left" Margin="210,63,0,0" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Margin="272,97,0,0" TextWrapping="Wrap" Text= "{Binding Mima}" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Margin="272,67,0,0" TextWrapping="Wrap" Text="{Binding ZhangHao}" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Margin="272,127,0,0" TextWrapping="Wrap" Name="test" VerticalAlignment="Top" Width="120"/>
<Button Content="登录" HorizontalAlignment="Left" Margin="272,161,0,0" VerticalAlignment="Top" Command="{Binding LoginAction}"/>
<Button Content="保存" HorizontalAlignment="Left" Margin="245,220,0,0" VerticalAlignment="Top" Width="180"/>
</Grid>
</Window>
2.创建监听文件
public class NotificationObject:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
---------------------------
public class ICommandClass:ICommand
{
readonly Func<bool> _CanExecute;
readonly Action _Execute;
public ICommandClass(Action action,Func<bool> func)
{
_Execute = action;
_CanExecute = func;
}
public bool CanExecute(object parameter)
{
return _CanExecute != null;
}
public void Execute(object parameter)
{
_Execute();
}
public event EventHandler CanExecuteChanged
{
add
{
if (_CanExecute != null)
{
CommandManager.RequerySuggested += value;
}
}
remove
{
if (_CanExecute != null)
{
CommandManager.RequerySuggested -= value;
}
}
}
}
3.创建model
public class ModelClass
{
public string _Name;
public string _Age;
public string _ZhangHao;
public string _Mima;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public string Age
{ get { return _Age; } set { _Age = value; } }
public string ZhangHao
{ get { return _ZhangHao; } set { _ZhangHao = value; } }
public string Mima
{ get { return _Mima; }set { _Mima = value; } }
}
4.创建viewmodel并绑定view与model
public class ViewModelClass : NotificationObject
{
private ModelClass _model;
private MainWindow _mainWindow;
public ViewModelClass(MainWindow mainWindow)
{
_mainWindow = mainWindow;
_model = new ModelClass();
}
public string ZhangHao
{
get { return _model.ZhangHao; }
set { _model.ZhangHao = value; RaisePropertyChanged("ZhangHao"); }
}
public string Mima
{
get { return _model.Mima; }
set { _model.Mima = value; RaisePropertyChanged("Mima"); }
}
public void LogionFunc()
{
if (ZhangHao == "wpf" && Mima == "666")
{
MessageBox.Show("登录成功");
}
else
{
MessageBox.Show("输入的用户名密码不正确");
ZhangHao = "";
Mima = "";
}
}
public bool CanExecute()
{
return true;
}
public ICommand LoginAction
{
get { return new ICommandClass(LogionFunc, CanExecute); }
}
}