
- 添加MvvmLight引用,通过Nuget;
- 加载nuget以后会有ViewModelLocator.cs;
- 新建自己的ViewModel,继承ViewModelBase;
- View 通过资源引用Viewmodel :
<UserControl.DataContext>
<Binding Path="CommunicaitonPara" Source="{StaticResource Locator}"></Binding>
</UserControl.DataContext>这是因为已经将ViewModelLocator设置为资源了。
- ViewModel写法
private string _ipAddress = "192.168.2.186";
public string IpAddress
{
get { return _ipAddress; }
set{Set(ref _ipAddress,value);}
} - 剩下就是正常的引用方式了
参考: