我有一个显示选项对话框的应用程序。 在该选项对话框中,我显示了独角兽列表。 当我选择一个独角兽时,我可以编辑或删除它。 当我想编辑独角兽时,它会在选项对话框上方显示另一个EditUnicorn对话框。 EditUnicorn对话框包含标签,每个标签都可以编辑独角兽的特定数据。使用Caliburn.Micro与Ninject和WPF在视图模型之间绑定/传递数据
Application
--> Options window showing unicorns (OptionsView)
----> edit unicorn dialog (EditUnicornView)
------> tabpages with usercontrols inside the edit unicorn dialog to fill in specific data about unicorn. (tabpages: EditUnicornSkillsView, EditUnicornFriendsView, EditUnicornGeneralView, ...)
在我的GUI麒麟模型,它实际上更多的是一个视图模型...
public class Unicorn
{
public string Name { get; set; }
public int Age { get; set; }
public string Strength { get; set; }
public Health HealthStatus { get; set; }
public List<Unicorn> Friends { get; set; }
}
public class OptionsViewModel : PropertyChangedBase
{
public ObservableCollection<Unicorn> Unicorns { get return MyData.Unicorns; }
private Unicorn _SelectedUnicorn;
public Unicorn SelectedUnicorn {
get { return _SelectedUnicorn; }
set {
_SelectedUnicorn = value;
NotifyOfPropertyChange(() => CanAddUnicorn);
NotifyOfPropertyChange(() => CanEditUnicorn);
NotifyOfPropertyChange(() => CanDeleteUnicorn);
}
}
public void EditUnicorn() {
// Is this correct?
WindowManager.ShowDialog(IoC.Get<EditUnicornViewModel(), SelectedUnicorn, null);
}
}
public class EditUnicornViewModel : Screen
{
// should it be like this? (or via the constructor or ...?)
public Unicorn Unicorn { get; set; }
}
的EditUnicornView.xaml包含:
<TabControl>
<TabItem Header="General">
<ContentControl x:Name="EditUnicornGeneralViewModel" />
</TabItem>
<TabItem Header="Skills">
<ContentControl x:Name="EditUnicornSkillsViewModel" />
</TabItem>
<TabItem Header="Friends">
<ContentControl x:Name="EditUnicornFriendsViewModel" />
</TabItem>
</TabControl>
为它们在用户控件中的ViewModels标签:
public class EditUnicornGeneralViewModel : PropertyChangedBase
{
public string Name { get; set; }
public int Age { get; set; }
}
public class EditUnicornSkillsViewModel : PropertyChangedBase
{
public string Strength { get; set; }
public Health HealthStatus { get; set; }
}
public class EditUnicornFriendsViewModel : PropertyChangedBase
{
public List<Unicorn> Friends { get; set; }
}
我在我的GUI应用程序中创建了一个独角兽模型类,它实际上更多的是一个视图模型,我创建了这个模型,因为tabpage中的每个用户控件都有一个特定的viewmodels来只显示必要的数据。我不确定我是否确实这样做是正确的。
现在的问题是,正如你所看到的EditUnicornViewModel(几乎)是空的。我如何将选定的Unicorn传递给EditUnicornViewModel。 如何将一个viewmodel的属性添加/注入/绑定/设置为另一个viewmodel的另一个属性? (ninject + caliburn.micro) 然后再次出现同样的问题:如何在EditUnicornViewModel中的每个EditUnicorn(General/Skills/Friends)ViewModel中设置特定的Unicorn字段?
编辑:
我不认为这是工作的正确方法(然后我仍然没有线索如何使用的TabPages做到这一点):
public class OptionsViewModel : PropertyChangedBase
{
// ...
public void EditUnicorn()
{
var vm = IoC.Get<EditUnicornViewModel>();
vm.Unicorn = SelectedUnicorn;
WindowManager.ShowDialog(vm, null, null);
}
}