我想用Caliburn.Micro+wpf项目解决Master->Detail问题。
我在TaskManagerView.xaml:中有两个像这样编码的comboboxes
<ComboBox x:Name="ProjectsList" SelectedValuePath="{Binding Path=SelectedProjectsList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox x:Name="TasksList"/>
第一个组合框ProjectsList
工作正常,我可以得到我想要的。第二个有点复杂。我正确地得到了SelectedProjectList
(当我在按钮上调试时,我看到我选择了什么(。ProjectsList
是new Project()
类中项目的名称。在Project
中,我存储了Task
的列表。但当我想填充TasksList
时,我会遇到异常。
现在,我的TaskManagerViewModel:
class TaskManagerViewModel : Screen //constructor of ViewModel
{
public BindableCollection<string> ProjectsList { get; set; } //first CB
public BindableCollection<string> TasksList { get; set; } //secondCB
public string SelectedProjectsList { get; set; }//selected item from first combobox
public TaskManagerViewModel(FakeData context) //constructor
{
this.context = context;
//it works fine, i get Project.Name here from List<Project>
ProjectsList = context.GetProjectsName();
//i selected project name from combobox,
//now i want to get tasks stored in List<Project>
//where SelectedProjectsList == Project.Name
tasks = context.GetProjectsTasks(SelectedProjectsList);
foreach (Task task in tasks)
{
TasksList.Add(task.TaskName);
NotifyOfPropertyChange(() => TasksList);
}
}
public void CancelButton() => TryClose();
private FakeData context;
private List<Task> tasks;
}
我的问题是,代码助手在哪里以及如何获取List<Task>'s
名称?我知道我在这里做的很糟糕,因为我在ViewModel的构建中做了所有的事情,但我想向你展示我想做什么。
现在,我得到NullReferenceException
,因为SelectedProjectsList
为空。如何在ComboBox
中的选定项目上引发事件?我选择了项目名称,我立即想从选择的项目中获得任务(我知道我可以很容易地从按钮中完成,但这不是我想要实现的(
谢谢你的建议!
编辑:
当然,如果我将VM编辑为这种形式:
class TaskManagerViewModel : Screen
{
public BindableCollection<string> ProjectsList { get; set; }
public BindableCollection<string> TasksList{ get; set; }
public string SelectedProjectsList { get; set; }
public TaskManagerViewModel(FakeData context)
{
this.context = context;
ProjectsList = context.GetProjectsName();
TasksList = new BindableCollection<string>();
//i moved initializing new combobox to AcceptButton()
}
public void AcceptButton()
{
//initializing tasks list on button click
tasks = context.GetProjectsTasks(SelectedProjectsList);
TasksList = new BindableCollection<string>();
foreach (Task task in tasks)
{
TasksList.Add(task.TaskName);
NotifyOfPropertyChange(() => TasksList);
}
}
public void CancelButton() => TryClose();
private FakeData context;
private List<Task> tasks;
}
我想做的是从第一个组合框中选择一个项目,然后立即显示第二个中的任务列表。我不想从第一个组合框中选择项目,点击按钮,然后从第二个组合框中选择项目
由于这是MVVM,您的ViewModel不知道组合框或其事件,因此您不能直接订阅控件的事件。
当你在项目列表中选择一个项目时,它会为SelectedProjectsList属性设置一个值,你可以使用这个:
private string _selectedProjectsList;
public string SelectedProjectsList
{
get
{
return _selectedProjectsList;
}
set
{
_selectedProjectsList = value;
// project selection has been changed, populate your task list now!
// upd: actual code for populating the list
tasks = context.GetProjectsTasks(SelectedProjectsList);
TasksList = new BindableCollection<string>();
foreach (Task task in tasks)
{
TasksList.Add(task.TaskName);
}
NotifyOfPropertyChange(() => TasksList);
}
}