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 ...
随机推荐
- C#中的枚举器(转)
术语表 Iterator:枚举器(迭代器) 如果你正在创建一个表现和行为都类似于集合的类,允许类的用户使用foreach语句对集合中的成员进行枚举将会是很方便的.这在C# 2.0中比 C# 1.1更容 ...
- HAPROXY 配置项/配置实例
HAPROXY 配置项/实例 常用配置选项: OPTION 选项: option httpclose :HAProxy会针对客户端的第一条请求的返回添加cookie并返回给客户端,客户端发送后续请求时 ...
- C 中typedef 函数指针的使用
类型定义的语法可以归结为一句话:只要在变量定义前面加上typedef,就成了类型定义.这儿的原本应该是变量的东西,就成为了类型. int integer; //整型变量int *pointer ...
- iOS开发——C篇&结构体与枚举
一:结构体与枚举的介绍: 结构体与枚举:是一种存储复杂的数据结构体:是用户自定义的一种类型,不同类型的集合,而数组是相同类型变量的集合. 二:结构体的创建 struct user { char ...
- cat查看proc下文件帮助
cat boot_mode 查看cpu版本 cat cmdline cat cpuinfo 查看cup详细信息 cat devices cat diskstats cat dma-mappings c ...
- Spark Streaming Backpressure分析
1.为什么引入Backpressure 默认情况下,Spark Streaming通过Receiver以生产者生产数据的速率接收数据,计算过程中会出现batch processing time > ...
- 如何升级nodejs版本
直接下载最新版安装即可,例如我本地的nodejs版本为: y@y:untitled$ node -v v0.10.33 当前node官网最新版本为:Current Version: v0.12.2 下 ...
- 51单片机C语言学习笔记5:include的区别
#include <iostream.h>#include "myfile_h" #include 是预处理器标识符.<>表示是标准的工程.标准的头文件.查 ...
- QT的文本加密方法(寒山居士)
http://blog.csdn.net/esonpo/article/details/12746315http://blog.csdn.net/esonpo/article/details/1174 ...
- android SimpleCursorAdapter的使用
String[] fields=new String[]{"foodname","price","taste","stuff&qu ...