如何在此代码中放置保护属性
本文关键字:保护 属性 代码 | 更新日期: 2023-09-27 18:36:44
我现在正在学习MVVM。由于我看到许多教程或项目仅使用视图和视图模型,因此我有点困惑。这是我的代码。
型:
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>
视图模型 :
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 模式指向正确的方式吗?
一种解决方案是从 PropertyChangedBase 继承视图模型并订阅 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;
}
}
}
希望它有效。
通知事件应该存在于view model
中,因为它传达来自model
的更改并将它们推送到 UI,反之亦然。这是根据 MVVM 设计指南。
在特定情况下,您可以或保持原样,但删除不必要的view model
,或将通知程序移入view model
您的通知更改应该在模型中,因为这也是您绑定的内容。 基本上,视图模型应该加载对象列表,并为大多数事情提供 UI 的绑定集合。 您上面的实现似乎是具有单个对象(例如编辑模式)的详细面板。
总结一下。
视图模型 - 应实现 InotifyPropertyChanged 接口模型 - 应实现 InotifyPropertyChanged 接口
要启用/禁用命令/按钮,您应该使用命令绑定。 我有一个关于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/2011/02/08/using-reflection-with-wpf-and-the-inotifypropertychanged-interface/
http://tsells.wordpress.com/2010/06/02/wpf-model-view-viewmodel-m-v-vm-example/
,我的代码
中有一个结论这是我的灵感
@tsells -- 感谢第 3 个链接
@john 波尔沃拉 - 感谢您指出该物业
重要:它应该是公共财产
保护条款不触发
与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>
视图模型 :
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; }
}
}