整个措施如下,从博客园一个作者看到的例子,但是对这个例子做了点改削。我感受这个更切合MVVM模式。这个用到了prism框架,在项目中要引用Microsoft.Practices.Prism.dll
凭据措施开发挨次记录如下法式:
一、先设计界面,这样才知道有哪些Model。
相应的xaml代码如下:
<Window x:Class="PrismMvvmExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<Label Content="学号" HorizontalAlignment="Left" Margin="54,23,0,0" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentId}" IsReadOnly="True" HorizontalAlignment="Right" Margin="0,27,289,0" VerticalAlignment="Top" />
<Label Content="姓名" HorizontalAlignment="Left" Margin="54,61,0,0" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentName}" IsReadOnly="True" HorizontalAlignment="Left" Margin="94,65,0,0" VerticalAlignment="Top" />
<Label Content="春秋" HorizontalAlignment="Left" Margin="54,94,0,0" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentAge}" IsReadOnly="True" HorizontalAlignment="Left" Margin="94,99,0,0" VerticalAlignment="Top" />
<Label Content="Email" HorizontalAlignment="Left" Margin="50,138,0,0" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentEmail}" IsReadOnly="True" HorizontalAlignment="Left" Margin="94,141,0,0" VerticalAlignment="Top" />
<Label Content="性别" HorizontalAlignment="Left" Margin="57,176,0,0" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentSex}" IsReadOnly="True" HorizontalAlignment="Left" Margin="94,180,0,0" VerticalAlignment="Top" />
<Button Command="{Binding ShowCommand}" Content="显示" HorizontalAlignment="Left" Margin="345,27,0,0" VerticalAlignment="Top" />
</Grid>
</Window>
二、然后就是从UI界面抽象出Model。
StudentModel.cs:
public class StudentModel
{
/// <summary>
/// 学号
/// </summary>
public int StudentId
{
get;
set;
}
/// <summary>
/// 姓名
/// </summary>
public string StudentName
{
get;
set;
}
/// <summary>
/// 春秋
/// </summary>
public int StudentAge
{
get;
set;
}
/// <summary>
/// Email
/// </summary>
public string StudentEmail
{
get;
set;
}
/// <summary>
/// 性别
/// </summary>
public string StudentSex
{
get;
set;
}
}
三、接下来就是ViewModel了。
StudentViewModel.cs:
public class StudentViewModel:NotificationObject
{
public DelegateCommand ShowCommand { get; set; }
public StudentViewModel()
{
ShowCommand = new DelegateCommand(new Action(ShowStudentInfo));
}
private StudentModel student;
public StudentModel Student
{
get { return student; }
set
{
student = value;
this.RaisePropertyChanged("Student");//Student这个StudentModel类的东西转变
}
}
public StudentModel StudentTemp { get; set; }
private void LoadData()
{
StudentTemp = new StudentModel();
StudentTemp.StudentId = 1;
StudentTemp.StudentName = "tina";
StudentTemp.StudentAge = 20;
StudentTemp.StudentEmail = "[email protected]";
StudentTemp.StudentSex = "大帅哥姐";
}
private void ShowStudentInfo()
{
#region --正确的方法--
//this.Student = StudentTemp;
this.LoadData();
//要东西转变,那么也要用一个东西赋值给它,让他转变,即用东西转变东西
this.Student = StudentTemp;
#endregion
#region --无效的方法,措施只认为改酿成员,并没有转变东西--
//Student = new StudentModel();
//Student.StudentId = 1;
//Student.StudentName = "tina";
//Student.StudentAge = 20;
//Student.StudentEmail = "[email protected]";
//Student.StudentSex = "大帅哥姐";
#endregion
}
}
最后就是把ViewModel和View关联起来:
MainWindow.xaml.cs:
从一个prismWpfMVVM的例子中学到的
,