WPF采用MVVM模式(绑定:纯前台、命令:触发器绑定命令)

时间:2022-01-07 16:59:50

MVVM绑定

view-viewModel-model,模型介绍省略,就是创建类,添加字段封装属性。注:控件的绑定只能绑定到属性上,不能绑定到字段上;

接下来就是代码

(view):

 <Window x:Class="WpfBing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfBing"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
Title="MainWindow" Height="" Width="">
<Grid>
<Grid.DataContext>
<vm:ViewModel/>
</Grid.DataContext>
<TextBox Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}" Width="" Height="">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding NameChanged}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<Button Content="测试" Command="{Binding UpdateData}" Width="" Height="" HorizontalAlignment="Right">
</Button>
</Grid>
</Window>

说明:

 xmlns:vm="clr-namespace:WpfBing"添加对命名空间的引用,主要是让前台页面能够寻找到viewmodel的命名空间;
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 添加wpf的命名控件引用,主要是用来提供使用该命名空间中的触发器绑定命令
该类的下载链接为:System.Windows.Interactivity
<Grid.DataContext>   <vm:ViewModel/> </Grid.DataContext> 数据源绑定,将该空间按的数据源绑定为vm空间下的ViewModel对象上;注:纯前台绑定的关键
 <i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding NameChanged}" />
</i:EventTrigger>
</i:Interaction.Triggers>
通过触发器实现对控件事件的命令绑定,该代码需要添加System.Windows.Interactivity.dll的引用

(BaseClass):

 using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input; namespace WpfBing
{
public class RelayCommand : ICommand
{
#region 字段
readonly Func<Boolean> _canExecute;
readonly Action _execute;
#endregion #region 构造函数
public RelayCommand(Action execute)
: this(execute, null)
{
} public RelayCommand(Action execute, Func<Boolean> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion #region ICommand的成员
public event EventHandler CanExecuteChanged
{
add
{ if (_canExecute != null)
CommandManager.RequerySuggested += value;
}
remove
{ if (_canExecute != null)
CommandManager.RequerySuggested -= value;
}
} [DebuggerStepThrough]
public Boolean CanExecute(Object parameter)
{
return _canExecute == null ? true : _canExecute();
} public void Execute(Object parameter)
{
_execute();
}
#endregion
}
}

说明:该段代码主要实现ICommand命令,实现该命令接口,通过委托调用调用ViewModel中相应的方法;

ICommand主要有两个方法,Excute,CanExcute,一个是调用的实现方法,一个是判断是否执行该调用方法;

注:功能上可以用来控制按钮或其他,控件状态是否可用

(viewmodel):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input; namespace WpfBing
{
public class ViewModel:INotifyPropertyChanged
{ public event PropertyChangedEventHandler PropertyChanged; public void Notify(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
} }
private string name = "测试数据"; public string Name
{
get { return name; }
set
{
name = value;
Notify("Name");
}
}
void UpdateArtistNameExecute()
{
this.Name = "中孝介";
} bool CanUpdateArtistNameExecute()
{
return true;
}
public ICommand UpdateData { get { return new RelayCommand(UpdateArtistNameExecute, CanUpdateArtistNameExecute); } }
public ICommand NameChanged { get { return new RelayCommand(NameChang); } } private void NameChang()
{
string na = Name;
}
}
}

说明:viewmodel中就是对一些事件流,数据流的控制了通过对数据,控制可以实现刷新前台数据,命令控制,可以访问业务层,等下层业务等;