項目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比較詳細的解釋
