2013-10-09 32 views
2

我有一个显示选项对话框的应用程序。 在该选项对话框中,我显示了独角兽列表。 当我选择一个独角兽时,我可以编辑或删除它。 当我想编辑独角兽时,它会在选项对话框上方显示另一个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); 

    } 
} 

回答

1

Caliburn.Micro带有一个强大的EventAggregator,它提供了一种干净的方式来传递系统中的数据而不假设任何人在监听。这将是您的最佳选择,因为这意味着N个标签可以收听和发送消息。见http://caliburnmicro.codeplex.com/wikipage?title=The%20Event%20Aggregator&referringTitle=Documentation

编辑:

我只想补充,采取通过文档很好看,Caliburn.Micro是根据各地的组成的想法,你不应该给自己打电话IOC.Get。基本上,你的应用程序应该撰写下来栈像这样

Shell > Conductor > Conducted ViewModels 

看看在回购的样品一样,他们表现出了很多很酷的组成特点。

相关问题