WPF采用MVVM模式(绑定:纯前台、命令:触发器绑定命令)
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中就是对一些事件流,数据流的控制了通过对数据,控制可以实现刷新前台数据,命令控制,可以访问业务层,等下层业务等;
WPF采用MVVM模式(绑定:纯前台、命令:触发器绑定命令)的更多相关文章
- 【WPF】MVVM模式的3种command
原文:[WPF]MVVM模式的3种command 1.DelegateCommand 2.RelayCommand 3.AttachbehaviorCommand 因为MVVM模式适合于WPF和SL, ...
- WPF之MVVM模式讲解
WPF技术的主要特点是数据驱动UI,所以在使用WPF技术开发的过程中是以数据为核心的,WPF提供了数据绑定机制,当数据发生变化时,WPF会自动发出通知去更新UI. 恰当的模式可以让我们轻松达到“高内聚 ...
- 【转】【WPF】MVVM模式的3种command
1.DelegateCommand 2.RelayCommand 3.AttachbehaviorCommand 因为MVVM模式适合于WPF和SL,所以这3种模式中也有一些小差异,比如RelayCo ...
- WPF之MVVM模式(1)
MVVM模式 一.MVVM模式概述 MVVM Pattern : Model\View\ViewModel View:视图.UI界面 ViewModel:ViewModel是对Model的封装,通过一 ...
- MVVM模式下 DataTemplate 中控件的绑定
今天给ListBox中通过DataTemplate生成的Button绑定命令时,一开始Button始终找不到绑定的命令.现找到了正确的绑定方式,特来记录一下. 先上个正确的示例: <ListBo ...
- WPF之MVVM模式(2)
我们都想追求完美 Every view in the app has an empty codebehind file, except for the standard boilerplate cod ...
- WPF中MVVM模式的 Event 处理
WPF的有些UI元素有Command属性可以直接实现绑定,如Button 但是很多Event的触发如何绑定到ViewModel中的Command呢? 答案就是使用EventTrigger可以实现. 继 ...
- WPF之MVVM模式(3)
有种想写一个MVVM框架的冲动!!! 1.Model中的属性应不应该支持OnPropertyChanged事件? 不应该.应该有ViewModel对该属性进行封装,由ViewModel提供OnProp ...
- WPF中 MVVM模式的Slider Binding.
对于Button的Command的绑定可以通过实现ICommand接口来进行,但是Slider并没有Command属性. 另外如果要实现MVVM模式的话,需要将一些Method和Slider的Even ...
随机推荐
- Java导入证书失败Keystore was tampered with, or password was incorrect
keytool 错误: java.io.IOException: Keystore was tampered with, or password was incorrect 在进行证书相关操作, ...
- php把数组保存成文件格式
php把数组保存为文件格式的函数实例,或许有的还没听说过可以把数组保存成文件,其实这样做也是另有它用的,两种方法各有千秋,有兴趣的PHP爱好者敬请参阅: $file="./cache/fil ...
- 如何解决mysql数据库8小时无连接自动关闭
windows下打开my.ini,增加: interactive_timeout=28800000 wait_timeout=28800000 专家解答:MySQL是一个小型关系型数据库管理系统,由于 ...
- windows7下python环境变量配置
默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行或cmd里直接使用python命令. 1. 首先需要在系统中注册python环境变量:假设pyth ...
- ural 1106 Two Teams
http://acm.timus.ru/problem.aspx?space=1&num=1106 #include <cstdio> #include <cstring&g ...
- Qt中 QString 和int, char等的“相互”转换,关键是QString.toLocal8Bit().data();
Qt中 int ,float ,double转换为QString 有两种方法 1.使用 QString::number(); 如: long a = 63; QString s = QString:: ...
- @Valid springMVC bean校验不起作用及如何统一处理校验
SpringMVC 使用JSR-303进行校验 @Valid 使用注解 一.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-vali ...
- PL/SQL 0.几秒出结果,SQL效率一定高吗?
今天开发问我一个问题,PL/SQL很快出结果了,为什么应用还是很慢 create index F_AGT_SAVB_ACCTINFO_H_idx4 on F_AGT_SAVB_ACCTINFO_H ( ...
- 在什么情况下使用struct,struct与class的区别
Struct定义和使用 类是引用类型,是保存在托管堆中的.通过定义类,我们可以在数据的生存期上得到很高的灵活性,但是也会让程序的性能有一定的损失.虽然这种损失很小,但当我们只需要定义一个很小的结构时, ...
- ubuntu的web开发环境搭建
为了保持mac的干净整洁,决定用PD搭建一条web开发环境,记下整个过程. ubuntu 首先是操作系统,本着习惯就好的原则,选用了Ubuntu server 12.04.4版.系统的安装很简单,ht ...