项目URL:http://www.mvvmlight.net/

一、安装MVVMLight

在NuGet程序包中搜索MVVMLight,然后安装。

二、使用

安装完MVVMLight后项目中会自动生成ViewModel目录,并且目录中会生成ViewModelLocator.cs文件

App.xaml会自动添加代码,注册全局变量Locator

    <Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:WpfTest.ViewModel" />
</ResourceDictionary>
</Application.Resources>

建立Model目录放置model类,View目录放置view类

在Model下添加PeopleModel.cs,继承ObservableObject类,ObservableObject实现了INotifyPropertyChanged接口,所有PeopleModel的属性改变可以通知控件绑定属性

    public class PeopleModel : ObservableObject
{
private string name = "";
public string Name { get => name; set => Set(ref name, value); }
}

在ViewModel下添加PeopleViewModel.cs,继承ViewModelBase类,ViewModelBase类继承ObservableObject类

    public class PeopleViewModel : ViewModelBase
{
private PeopleModel people = new PeopleModel();
public PeopleModel People { get => people; set => Set(ref people, value); }
//不带参数命令
public ICommand CmdUpdateName
{
get
{
return new RelayCommand(new Action(() =>
{
People.Name = "无参数";
}));
}
}
//带参数命令
public ICommand CmdUpdateName1
{
get
{
return new RelayCommand<object>(new Action<object>(t =>
{
People.Name = System.Convert.ToString(t);
}));
}
}
} 

在ViewModelLocator.cs里注册PeopleViewModel元素

    /// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// </summary>
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); ////if (ViewModelBase.IsInDesignModeStatic)
////{
//// // Create design time view services and models
//// SimpleIoc.Default.Register<IDataService, DesignDataService>();
////}
////else
////{
//// // Create run time view services and models
//// SimpleIoc.Default.Register<IDataService, DataService>();
////} SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<PeopleViewModel>();
} public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
} public PeopleViewModel PeopleVM
{
get
{
return ServiceLocator.Current.GetInstance<PeopleViewModel>();
}
} public static void Cleanup()
{
// TODO Clear the ViewModels
}
}  

在View目录下添加PeopleWindow窗体,并给window的DataContext属性绑定PeopleVM元素

<Window x:Class="WpfTest.View.PeopleWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTest.View"
mc:Ignorable="d"
Title="PeopleWindow" Height="450" Width="800"
DataContext="{Binding Source={StaticResource Locator}, Path=PeopleVM}">
<Grid>
<Button Command="{Binding CmdUpdateName}" Content="无参数" HorizontalAlignment="Left" Margin="10,31,0,0" VerticalAlignment="Top" Width="75"/>
<Button Command="{Binding CmdUpdateName1}" CommandParameter="有参数" Content="有参数" HorizontalAlignment="Left" Margin="10,69,0,0" VerticalAlignment="Top" Width="75"/>
<Label Content="{Binding People.Name}" HorizontalAlignment="Left" Margin="135,33,0,0" VerticalAlignment="Top" RenderTransformOrigin="-1.663,-1.363"/>
</Grid>
</Window>

界面效果:

(1)点击无参数按钮:

(2)点击有参数按钮

参考:

https://www.cnblogs.com/manupstairs/p/4890300.html 里面有对MVVMLight比较详细的解释

最新文章

  1. (哈夫曼树)HuffmanTree的java实现
  2. IP地址,子网掩码、默认网关,DNS服务器是什么意思?
  3. 安卓跳转到GPS设置界面
  4. 解决 Django 后台上传图片前端无法展示
  5. reduce方法
  6. Python 列表元素排重uniq
  7. hdu 1251 字典树的应用
  8. Java基础——序列化
  9. UITableView表视图以及重建机制
  10. C++ Tempatet之模板模型
  11. 【转】Eclipse中一键调用javah生成jni的头文件
  12. ECMAScript 6 中的快捷语法汇总及代码示例
  13. OJ题解记录计划
  14. day37协程与线程套接字通讯
  15. Reference SoftReference WeakReference PhantomReference Cleaner 的研究与实践
  16. Java并发:线程间数据传递和交换
  17. HDUOJ-----2571跳舞毯
  18. WCF Data Services 5.0 for OData V3
  19. 【Android】17.0 UI开发(八)——利用RecyclerView列表控件实现精美的聊天界面
  20. javascript 异步操作,串形执行,并行执行

热门文章

  1. Bilibili用户需求分析报告
  2. centos6.5搭建rabbitmq服务器(单机)
  3. .net和ASP.net,c#的区别
  4. 在airflow的BashOperator中执行docker容器中的脚本容易忽略的问题
  5. Java基础---Java 开发工具IntelliJ IDEA 快捷操作
  6. Linux下安装redis 3.0及C语言中客户端实现demo
  7. git 显示文件改动,但无具体改动信息
  8. PAT(B)1015 德才论(C)
  9. SAS学习笔记42 宏程序
  10. 编写并提取通用 ShellCode