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 ...
随机推荐
- jQuery的map()与jQuery.map()总结
请注意他们不是同一个函数.前者是jQuery对象的实例方法(即$.fn.map),后者是一个仅仅挂在jQuery对象下的静态方法(即$.map). 他们用法的异同:map()的返回值是包裹了一个Arr ...
- $(this).val()与this.value的区别?text()与html()的区别?
$(this).val()与this.value 作用:都是获得当前Dom对象的value值(一般是表单元素) text radio checkbox select 基本没有什么区别,只是: this ...
- C++Primer charpter1.
一.输入输出流 endl:会刷新buffer.刷新之后你才能看到.不手动用endl的话,就只能依靠系统自动刷.程序崩溃的话,你看到的调试信息可能是错误的. >>: 两个连续的符号 ci ...
- 移植rom移动TD到联通W
1.修改build.prop TD为 ril.flightmode.poweroffMD=0 ril.telephony.mode=2 改为 ril.flightmode.poweroffMD=1 r ...
- 转:Durandal快速入门
Durandal是一个轻量级的JavaScript框架,其目标是单页面应用(SPAs)的开发变得简单而优雅.它支持MVC.MVP和MVVM等模式,因此不论你采用哪种类型的前端架构,Durandal都能 ...
- 温习PYTHON语法
看WEBPY的源码结构有点晕,原来很多语法结构都忘了,继承之些. 再看A BYTE OF PYTHON.慢慢补.. number = 23 guess = int(raw_input('Enter a ...
- PYTHON文件多线程下载
其实,在一般的文件编程中,这有两个概念要说明: 第一是,下载一个大文件,将这个大文件多为多线程. 第二是,下载N多小文件,将每个线程指定下载多个小文件. 现在实现的是多线程下载一个大文件. 今天完成了 ...
- 如何禁止KEIL初始化RAM为零& 如何判断是软复位还是上电复位
(1)如何禁止KEIL初始化RAM为零? 1. 在KEIL Noinit 打钩 2. <1> 另须对需要热启动保持的变量用 _at_ 关键字指定某个区域,否则还是没用 <2>或 ...
- 【HDOJ】2577 How to Type
DP. /* 2577 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MAX ...
- ubuntu配置bridge网桥
先安装uml-utilities,该工具包含建立虚拟网络设备(所谓的“TAP interfaces”)的工具: sudo apt-get install uml-utilities 安装 桥接工具 b ...