我用这个构造函数来初始化这个对象
ObservableCollection<MyHomeworkModel> allTabs = new ObservableCollection<MyHomeworkModel>();
原来 allTabs 初始化后为空。因此,我无法向其中添加 MyHomeworkModel 对象。
请帮助我,并提前感谢。
using System.Windows.Input;
using System.Collections.ObjectModel;
using MyHomework;
namespace MyHomework__MVVM_
{
class MyHomeworkViewModel
{
private ObservableCollection<MyHomeworkModel> allTabs;
private MyHomeworkModel selectedTab;
public MyHomeworkViewModel()
{
ObservableCollection<MyHomeworkModel> allTabs = new ObservableCollection<MyHomeworkModel>();
selectedTab = new MyHomeworkModel();
AddCourseCommand = new AddCourseCommand(this);
}
public ObservableCollection<MyHomeworkModel> AllTabs
{
get
{
return allTabs;
}
set
{
allTabs = value;
}
}
public MyHomeworkModel SelectedTab
{
get
{
return selectedTab;
}
set
{
selectedTab = value;
}
}
public ICommand AddCourseCommand
{
get;
private set;
}
public void AddNewTab()
{
NewCourseName ncn = new NewCourseName();
ncn.ShowDialog();
if (ncn.courseName != null)
{
MyHomeworkModel newTab = new MyHomeworkModel();
newTab.Header = ncn.courseName;
AllTabs.Add(newTab);
}
}
}
}