一.数据的绑定
1.1建立一个和主页面同名的Model文件,在ViewModel里写实现的方法
1.2具体的页面数据获取,需要通过Model传入,故在Model文件里添加MainModel.cs文件
二.具体代码
1.MainWondow.xaml
CommandParameter="{Binding ElementName=tb,Path=Text}">
2.MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
//添加了View与ViewModel之间的联系,控件的控制还是由VM中的Model来处理
this.DataContext = new MainViewModel();
}
3.MainModel.cs
internal class MainModel:INotifyPropertyChanged
{
//所有需要绑定到页面的都必须是属性
//根据交互需求,创建一个相关的属性
// public string Keyword { get; set; } = "hello";
private string _Keyword;
public string Keyword
{
get { return _Keyword; }
set
{
_Keyword=value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Keyword"));
}
}
public ICommand BtnCommand { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
4.MainViewModel.cs
namespace LeeThird.ViewModel
{
internal class MainViewModel
{
public MainModel MainModel { get; set; } = new MainModel();
public MainViewModel()
{
MainModel.BtnCommand = new Command()
{
//2.数据通过DoExecute传给Search函数
DoExecute = obj =>
{
this.Search(obj.ToString());
}
};
}
//
//从数据库查找关键词相同的数据
//获取关键词,根据关键词执行查找
public void Search(string keyword)
{
//3.Search函数被调用,执行相关操作
string ky = MainModel.Keyword;
MainModel.Keyword = " ";
}
}
class Command : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => true;
public void Execute(object parameter)
{
//1.Command执行的时候触发这个方法
DoExecute?.Invoke(parameter);
}
public Action
}



