我正在学习MVVM。由于我看过很多教程或项目只使用View和ViewModel,我有点困惑。这是我的代码。
模特:
public class StudentModel : PropertyChangedBase
{
private String _firstName;
public String FirstName
{
get { return _firstName; }
set
{
_firstName = value;
NotifyOfPropertyChange(() => FirstName);
}
}
private Double _gradePoint;
public Double GradePoint
{
get { return _gradePoint; }
set
{
_gradePoint = value;
NotifyOfPropertyChange(() => GradePoint);
}
}
}
查看:
<UserControl x:Class="MVVMLearningWithCaliburnMicro.Views.StudentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org">
<Grid Width="525" Height="300" Background="Lavender">
<DockPanel>
<TextBlock HorizontalAlignment="Center" Text="Student Data"
DockPanel.Dock="Top" FontSize="20" />
<StackPanel Orientation="Vertical" HorizontalAlignment="Center"
VerticalAlignment="Stretch"
Margin="0,8" DockPanel.Dock="Top">
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="Name" FontSize="15" Margin="5,0" />
<TextBox Name="txtName" Text="{Binding Path=Student.FirstName}" Width="250" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="Grade" FontSize="15" Margin="5,0" />
<TextBox Name="txtGrade" Text="{Binding Path=Student.GradePoint}" Width="250" />
</StackPanel>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,5" HorizontalAlignment="Center"
VerticalAlignment="Bottom"
DockPanel.Dock="Bottom">
<Button Name="btnSave" Width="100" Height="40"
cal:Message.Attach="SaveStudent">
<TextBlock Text="Save" FontSize="15" />
</Button>
</StackPanel>
</DockPanel>
</Grid>
</UserControl>
VIEWMODEL:
public class StudentViewModel
{
public StudentModel Student { get; set; }
public void SaveStudent()
{
MessageBox.Show(String.Format("Saved: {0} - ({1})", Student.FirstName, Student.GradePoint));
}
public StudentViewModel()
{
Student = new StudentModel { FirstName = "Tom Johnson", GradePoint = 3.7 };
}
private Boolean CanSaveStudent()
{
return Student.GradePoint >= 0.0 || Student.GradePoint <= 4.0;;
}
}
问:
1.我如何设置我的警卫财产,因为NotifyOfPropertyChange()
是在模型中?
2.(愚蠢的问题)我的MVVM模式是否指向正确的方式?
答案 0 :(得分:2)
一种解决方案是从PropertyChangedBase继承viewmodel并订阅StudentModel的属性更改。然后将guard方法转换为属性,如下所示:
public class StudentViewModel: PropertyChangedBase
{
public StudentModel Student { get; set; }
public void SaveStudent()
{
MessageBox.Show(String.Format("Saved: {0} - ({1})", Student.FirstName, Student.GradePoint));
}
public StudentViewModel()
{
Student = new StudentModel { FirstName = "Tom Johnson", GradePoint = 3.7 };
Student.PropertyChanged += delegate { NotifyOfPropertyChanged( () => CanSaveStudent)};
}
public Boolean CanSaveStudent
{
get
{
return Student.GradePoint >= 0.0 || Student.GradePoint <= 4.0;
}
}
}
希望它有效。
答案 1 :(得分:1)
通知事件应出现在view model
中,因为它会传递来自model
的更改并将其推送到用户界面,反之亦然。这是根据MVVM设计指南。
在您的具体情况下,您可以保留或保留原样,但删除不必要的view model
,或移动view model
答案 2 :(得分:0)
您的通知更改应该在模型中,因为这也是您的约束。基本上,您的视图模型应该加载对象列表,并为大多数事物提供绑定集合到UI。您上面的实现似乎是一个具有单个对象的详细面板(例如编辑模式)。
总结一下。
ViewModel - 应该实现INotifyPropertyChangeed接口 模型 - 应该实现INotifyPropertyChangeed接口
要启用/禁用命令/按钮,您应该使用命令绑定。我有一个你可以参考的WPF和Silverlight教程。
http://tsells.wordpress.com/2010/06/23/command-binding-with-wpf-and-silverlight-net-4-0-with-m-v-vm/
在过去的几年里,我还提出了一些关于这件事的教程。如果你愿意,请检查出来......
http://tsells.wordpress.com/2010/06/02/wpf-model-view-viewmodel-m-v-vm-example/
答案 3 :(得分:0)
好的,我的代码中有一个结论 这是我的灵感
@tsells - 感谢第3个链接
@john polvora - 感谢指出该物业
重要提示:应该是公共财产
Guard Clause Not Firing
Error on guard clause with Caliburn.Micro
这里的代码我认为更清洁
模特:
public class StudentModel
{
public String FirstName { get; set; }
public Double GradePoint { get; set; }
}
查看:
<UserControl x:Class="MVVMWithCMTwo.Views.StudentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cal="http://www.caliburnproject.org">
<Grid Width="525" Height="300" Background="Lavender">
<DockPanel>
<TextBlock HorizontalAlignment="Center" Text="Student Data"
DockPanel.Dock="Top" FontSize="20" />
<StackPanel Orientation="Vertical" HorizontalAlignment="Center"
VerticalAlignment="Stretch"
Margin="0,8" DockPanel.Dock="Top">
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="Name" FontSize="15" Margin="5,0" />
<TextBox Name="txtName" Text="{Binding Path=StudentName}" Width="250" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="Grade" FontSize="15" Margin="5,0" />
<TextBox Name="txtGrade" Text="{Binding Path=StudentGrade}" Width="250" />
</StackPanel>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,5" HorizontalAlignment="Center"
VerticalAlignment="Bottom"
DockPanel.Dock="Bottom">
<Button Name="btnSave" Width="100" Height="40"
cal:Message.Attach="SaveStudent">
<TextBlock Text="Save" FontSize="15" />
</Button>
</StackPanel>
</DockPanel>
</Grid>
</UserControl>
VIEWMODEL:
public class StudentViewModel : PropertyChangedBase
{
public StudentModel Student { get; set; }
public String StudentName
{
get { return Student.FirstName; }
set
{
Student.FirstName = value;
NotifyOfPropertyChange(() => Student.FirstName);
}
}
public Double StudentGrade
{
get { return Student.GradePoint; }
set
{
Student.GradePoint = value;
NotifyOfPropertyChange(() => Student.GradePoint);
NotifyOfPropertyChange(() => CanSaveStudent);
}
}
public void SaveStudent()
{
MessageBox.Show(String.Format("Saved: {0} - ({1})", Student.FirstName, Student.GradePoint));
}
public StudentViewModel()
{
Student = new StudentModel { FirstName = "Tom Johnson", GradePoint = 3.7 };
}
public Boolean CanSaveStudent
{
get { return Student.GradePoint >= 0.0 && Student.GradePoint <= 4.0; }
}
}