我对 MVVM 还是很陌生,需要一些指导。
我已经设置了以下 MVVM,并且在使用时可以正常工作:
private Model model = new Model();
但是,我实际上想要收集模型。public ObservableCollection<Model> models = new ObservableCollection<Model>();
我现在不太清楚的是如何让属性返回例如绑定(bind)/选定模型的标题。我无法指定索引,因为我还不知道索引。我在想在 View 中,我会添加一个隐藏的组合框,当窗口加载时,创建一个新模型,并选择最高的索引。但是如何确保返回正确的属性以及如何对其进行编码?
当前设置
查看
View 绑定(bind)到 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 和两个模型。
类(class)_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;
}
关于c# - 如何在 ViewModel 中设置从模型列表中获取和设置值的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63404695/