• Model层
 public class Song
{
private string _artistName;
private string _songTitle; public string SongTitle
{
get { return _songTitle; }
set { _songTitle = value; }
} public string ArtistName
{
get { return _artistName; }
set { _artistName = value; }
} }
  • ViewModel

RelayCommand

 public class RelayCommand : ICommand
{ private readonly Func<Boolean> _canExecute; private readonly Action<object> _execute; public RelayCommand(Action<object> execute)
: this(execute, null)
{ } public RelayCommand(Action<object> execute, Func<bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute is null");
_canExecute = canExecute;
_execute = execute;
} public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute();
} public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
{
CommandManager.RequerySuggested += value;
}
}
remove
{
if (_canExecute != null)
{
CommandManager.RequerySuggested -= value;
}
}
} public void Execute(object parameter)
{
_execute(parameter);
}
}
 public sealed class SongVM : INotifyPropertyChanged
{
private Song _song; public SongVM()
{
_song = new Song() { SongTitle = "叮叮当", ArtistName = "wjp" };
} public Song Song
{
get { return _song; }
set
{
_song = value;
}
} public string ArtistName
{
get { return Song.ArtistName; }
set
{
if (ArtistName != value)
{
Song.ArtistName = value;
RaisePropertyChanged("ArtistName");
}
}
} public string SongTitle
{
get { return Song.SongTitle; }
set
{
if (SongTitle!=value)
{
Song.SongTitle = value;
RaisePropertyChanged("SongTitle");
}
}
} public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
} private void Execute(object para)
{
if (para != null)
ArtistName = para.ToString();
} private bool CanExeCute()
{
return true;
} public ICommand UpdateAtistName
{
get
{
return new RelayCommand(Execute, CanExeCute);
}
}
}
  • View
<Window x:Class="MVVMLightDemo.View.SampleMVVM"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:localVM="clr-namespace:MVVMLightDemo.ViewModel"
Title="SampleMVVM" Height="" Width="">
<Window.DataContext>
<localVM:SongVM/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="" Grid.Row="" Text="姓名" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.Column="" Grid.Row="" Text="歌曲名称" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.Column="" Grid.Row="" Text="{Binding ArtistName}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.Column="" Grid.Row="" Text="{Binding Song.SongTitle}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Button Grid.Column="" Grid.ColumnSpan="" Grid.Row="" Margin="" Content="更新姓名" Command="{Binding UpdateAtistName}" CommandParameter="王俊鹏"/>
</Grid>
</Window>

简易的WPF MVVM模式开发的更多相关文章

  1. WPF MVVM模式的一些理解

    /*本文转自 http://www.cnblogs.com/sirkevin/archive/2012/11/28/2793471.html */ 使用WPF+Mvvm开发一年多,期间由于对Mvvm模 ...

  2. WPF自学入门(十一)WPF MVVM模式Command命令 WPF自学入门(十)WPF MVVM简单介绍

    WPF自学入门(十一)WPF MVVM模式Command命令   在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式 ...

  3. WPF MVVM模式中,通过命令实现窗体拖动、跳转以及显隐控制

    原文:WPF MVVM模式中,通过命令实现窗体拖动.跳转以及显隐控制 在WPF中使用MVVM模式,可以让我们的程序实现界面与功能的分离,方便开发,易于维护.但是,很多初学者会在使用MVVM的过程中遇到 ...

  4. 用MVVM模式开发中遇到的零散问题总结(4)——自制摄像头拍摄大头贴控件

    原文:用MVVM模式开发中遇到的零散问题总结(4)--自制摄像头拍摄大头贴控件 一直有个疑问,为什么silverlight对摄像头支持这么好,WPF却一个库都没有....于是我各种苦恼啊,各种Code ...

  5. 用MVVM模式开发中遇到的零散问题总结(5)——将动态加载的可视元素保存为图片的控件,Binding刷新的时机

    原文:用MVVM模式开发中遇到的零散问题总结(5)--将动态加载的可视元素保存为图片的控件,Binding刷新的时机 在项目开发中经常会遇到这样一种情况,就是需要将用户填写的信息排版到一张表单中,供打 ...

  6. wpf mvvm模式下CommandParameter传递多参

    原文:wpf mvvm模式下CommandParameter传递多参 CommandParameter一般只允许设置一次,所以如果要传递多参数,就要稍微处理一下.我暂时还没找到更好的方案,下面介绍的这 ...

  7. 用MVVM模式开发中遇到的零散问题总结(2)

    原文:用MVVM模式开发中遇到的零散问题总结(2) 本节目录: 1.解决动画属性被劫持问题 2.设置页面焦点默认所在对象 3.XAML模拟键盘按键 4.DataGrid数据源绑定到复杂格式(dynam ...

  8. 用MVVM模式开发中遇到的零散问题总结(3)——自制正则表达式万能绑定转换器

    原文:用MVVM模式开发中遇到的零散问题总结(3)--自制正则表达式万能绑定转换器 前言 最近接受了3个项目的洗礼,出差近3个月,各种北京.广州.昆明来回奔波,好久没写博客了,之前我觉得我遇到的问题都 ...

  9. C# WPF开发之MVVM模式开发

    MVVM模式由Model,View,ViewModel三部分组成. Model需继承INotifyPropertyChange(属性修改通知) ViewModel负责业务逻辑,连接View和Model ...

随机推荐

  1. CSS3 基础知识

    CSS3 基础知识1.边框    1.1 圆角  border-radius:5px 0 0 5px;    1.2 阴影  box-shadow:2px 3px 4px 5px rgba(0,0,0 ...

  2. 图文-水平垂直居中兼容ie6+

    图文-水平垂直居中兼容ie6+ 具体代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...

  3. js中constructor的作用

    在学习过程中对js的constructor的作用产生了疑问.下面是学习的资料进行梳理 function Person(area){ this.type = 'person'; this.area = ...

  4. win7自由调整CMD窗口

    有如下命令,只需要改动相关参数即可以任意改变cmd窗口大小. mode con lines= mode con cols= color cls @cmd

  5. nodejs+express Mvc站点

    nodejs+express Mvc站点 像asp.net Mvc一样开发nodejs+express Mvc站点 首先,我是个c#码农.从事Mvc开发已然4个年头了,这两年前端MVC的兴起,我也跟风 ...

  6. 使用UEditor

    在http://ueditor.baidu.com/website/上下载官方文件 文本编辑器的配置文件在ueditor.config.js 需要注意一下几点 首先 var URL = window. ...

  7. Database JDBC Developer's Guide

    http://docs.oracle.com/database/121/JJDBC/datacc.htm#JJDBC28363

  8. Linux 程序设计的一些优化措施

    Linux 程序设计的一些优化措施 这些知识是在平常的阅读中,零散的获得的,自己总结了一下,分享在这里 全局变量VS函数参数 全局变量在Linux下的驱动编程里边,用的是非常多,例如中断服务函数ISR ...

  9. Linux TCP/IP 协议栈之 Socket 的实现分析(一)

    内核版本:2.6.37参考[作者:kendo的文章(基于内涵版本2.6.12)] 第一部份 Socket套接字的创建 socket 并不是 TCP/IP协议的一部份. 从广义上来讲,socket 是U ...

  10. WPF最基本的4个引用

    Windowsbase Windows基本类库 PresentationCore wpf核心类库 PresentationFramework wpf框架 System.Axml 系统类库