MVVM test
2024-10-02 06:35:02
public class RegisterUserViewModel
{
public UserInfo userInfo { get; set; } public ICommand ClickCommand { get; set; } public RegisterUserViewModel()
{
userInfo = new UserInfo();
userInfo.Age = ;
this.ClickCommand = new DelegateCommand<object>(OnClick, arg => true);
} void OnClick(object obj)
{
userInfo.Age += ;
}
}
ViewModel
<Window x:Class="mvvmTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Grid>
<Button Height="" HorizontalAlignment="Left" Margin="12,0,0,72" Name="btnClick" VerticalAlignment="Bottom" Width="" Command="{Binding ClickCommand}">Click me</Button>
<TextBlock Height="" Margin="12,44,0,0" Name="textBlock1" VerticalAlignment="Top" Text="UserName:" HorizontalAlignment="Left" Width="" />
<TextBlock Margin="12,93,0,0" Name="textBlock2" Height="" HorizontalAlignment="Left" VerticalAlignment="Top" Width="" Text="Age:" />
<TextBlock Height="" Margin="99,44,117,0" Name="tbUserName" Text="" VerticalAlignment="Top" />
<TextBlock Height="" Margin="99,93,117,0" Name="tbAge" Text="{Binding userInfo.Age}" VerticalAlignment="Top" />
</Grid>
</Window>
Xaml View
public partial class MainWindow : Window
{ private RegisterUserViewModel viewModel; public MainWindow()
{
InitializeComponent(); this.ViewModel = new RegisterUserViewModel(); } public RegisterUserViewModel ViewModel
{
get
{
return this.viewModel;
}
set
{
this.viewModel = value;
this.DataContext = value;
}
}
}
View cs
public class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
} public class UserInfo : NotificationObject
{
private int age; public int Age
{
get
{
return age;
} set
{
age = value;
this.RaisePropertyChanged("Age");
}
}
}
Model
最新文章
- Mysql调试存储过程最简单的方法
- UIBezierPath贝塞尔弧线常用方法记
- Ubuntu 系统密码相关问题
- Git合并开发代码分支到测试代码分支
- js中String.prototype.format類似于.net中的string.formitz效果
- python学习--抓取一个网页上图片
- hadoop拾遗(五)---- mapreduce 输出到多个文件 / 文件夹
- C++ 之高效使用STL ( STL 算法分类)
- Web网站与Web应用程序区别
- VisualStudio2013快捷键
- nginx前端负载,后端apache获取真实IP设置
- 百度之星2017初赛A轮 1001 小C的倍数问题
- 物体自由落体动态模拟(Linear Subspace)
- 基于redis的延迟消息队列设计
- 【转载】基于VoiceOver的移动web站无障碍访问实战
- css实现椭圆、半椭圆
- python_字符串的操作
- Spring cloud和Dubbo
- 从零开始编写自己的JavaScript框架(一)
- HTTP 接口响应数据解析