问题描述
我对MVVM还是很陌生,需要一些指导。
我已经设置了以下MVVM,并且在使用时可以正常工作
private Model model = new Model();
但是,我实际上想收集模型。
public ObservableCollection<Model> models = new ObservableCollection<Model>();
我无法指定索引,因为我还不知道索引。我正在考虑在视图中添加一个隐藏的组合框,并在加载窗口时创建一个新模型并选择最高索引。但是,如何确保返回正确的属性以及如何对其进行编码?
当前设置
查看
该视图按如下方式绑定到viewmodel。
<Window.DataContext>
<this:viewmodel />
</Window.DataContext>
viewmodel由两个类组成。一个父类,为当前View实现INotifyPropertyChanged和viewmodel。
using System.ComponentModel;
namespace Framework
{
public abstract class ObservableObject : INotifyPropertyChanged
{
#region Events
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Implementation
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
using Framework;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Windows;
namespace ESL_Master_Suite.Components.Core.Courses
{
/// <summary>
/// Interaction logic for Courses.xaml
/// </summary>
public partial class Courses : Window
{
public Courses()
{
InitializeComponent();
}
}
class viewmodel : ObservableObject
{
public ObservableCollection<Model> models = new ObservableCollection<Model>();
//private Model model = new Model();
#region Fields
bool skipUpdating = false;
#endregion
#region Properties
public string Title
{
get
{
return model.title;
}
set
{
model.title = value;
NotifyPropertyChanged("Title");
}
}
#endregion
}
模型/模型周
模型和子模型如下所示。
class Model
{
public string title;
public ObservableCollection<Model_Weeks> weeks = new ObservableCollection<Model_Weeks>();
}
class Model_Weeks
{
[DataMember(Name = "title")]
public string title { get; set; }
[DataMember(Name = "topic")]
public int topic { get; set; }
}
}
解决方法
经过大量的努力,我提出了以下解决方案。我不确定这是否是正确的方法,但似乎可行。
我有两个ViewModel和两个模型。
Courses_ViewModel
class Courses_ViewModel : ObservableObject
{
public Courses_Model courses_Model;
public ObservableCollection<Course_ViewModel> course_ViewModels { get; set; }
public Courses_ViewModel()
{
//Deserialize and assign Parent Model to Local Variable
courses_Model = new Courses_Model();
//Initialise ChildModel List
course_ViewModels = new ObservableCollection<Course_ViewModel>();
//Create ChildViewModels and Add to List
foreach (Course_Model course in courses_Model.course_Models)
{
course_ViewModels.Add(new Course_ViewModel(course));
}
}
}
Course_ViewModel
class Course_ViewModel : ObservableObject
{
public Course_Model course_Model { get; set; }
public Course_ViewModel(Course_Model model)
{
course_Model = model;
}
public string Title
{
get
{
return course_Model.title;
}
set
{
course_Model.title = value;
NotifyPropertyChanged("Title");
}
}
}
模型
class Courses_Model
{
//List of courses
public List<Course_Model> course_Models { get; set; }
public Courses_Model()
{
//Initialise course_Models List
course_Models = new List<Course_Model>();
//Create courses and add to list
course_Models.Add(new Course_Model { title = "L1" });
course_Models.Add(new Course_Model { title = "L2" });
course_Models.Add(new Course_Model { title = "L3" });
course_Models.Add(new Course_Model { title = "L4" });
course_Models.Add(new Course_Model { title = "L5" });
}
}
class Course_Model
{
public string title;
}