2013-10-09 36 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 

看看在回購的樣品一樣,他們表現出了很多很酷的組成特點。

相關問題