WPF采用MVVM模式命令绑定
程序员文章站
2022-06-07 15:13:55
...
<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="350" Width="525">
<Grid>
<Grid.DataContext>
<vm:ViewModel/>
</Grid.DataContext>
<TextBox Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding NameChanged}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<Button Content="测试" Command="{Binding UpdateData}" Width="150" Height="40" 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的引用
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,一个是调用的实现方法,一个是判断是否执行该调用方法;
注:功能上可以用来控制按钮或其他,控件状态是否可用
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;
}
}
}
上一篇: 练习题010:判断一个数是否为水仙花数
下一篇: WPF编程,自定义鼠标形状的一种方法。