從一個prismWpfMVVM的例子中學到的
整個程序如下,從博客園一個作者看到的例子,但是對這個例子做了點修改。我覺得這個更符合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" Height="350" Width="525">
<Grid>
<Label Content="學號" Height="28" HorizontalAlignment="Left" Margin="54,23,0,0" Name="labelStudentId" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentId}" IsReadOnly="True" Height="23" HorizontalAlignment="Right" Margin="0,27,289,0" Name="textBoxStudentId" VerticalAlignment="Top" Width="120" />
<Label Content="姓名" Height="28" HorizontalAlignment="Left" Margin="54,61,0,0" Name="labelStudentName" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentName}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,65,0,0" Name="textBoxStudentName" VerticalAlignment="Top" Width="120" />
<Label Content="年齡" Height="28" HorizontalAlignment="Left" Margin="54,94,0,0" Name="labelStudentAge" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentAge}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,99,0,0" Name="textBoxStudentAge" VerticalAlignment="Top" Width="120" />
<Label Content="Email" Height="28" HorizontalAlignment="Left" Margin="50,138,0,0" Name="labelStudentEmail" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentEmail}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,141,0,0" Name="textBoxStudentEmail" VerticalAlignment="Top" Width="120" />
<Label Content="性別" Height="28" HorizontalAlignment="Left" Margin="57,176,0,0" Name="labelStudentSex" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentSex}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,180,0,0" Name="textBoxStudentSex" VerticalAlignment="Top" Width="120" />
<Button Command="{Binding ShowCommand}" Content="顯示" Height="23" HorizontalAlignment="Left" Margin="345,27,0,0" Name="buttonShow" VerticalAlignment="Top" Width="75" />
</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的例子中學到的