2015-04-26 61 views
2

將HubPage部分中定義的視圖綁定到相應的ViewModels需要什麼流程?使用CaliburnMicro在HubPage中綁定ViewModel

<Hub Header="{Binding HubHeader}" > 
    <HubSection x:Name="NewestOffers" Header="{Binding NewestOffersHeader}" IsHeaderInteractive="True" > 
     <DataTemplate > 
      <local:NewestOffersView DataContext="{Binding NewestOffers}"/> 
     </DataTemplate> 
    </HubSection> 

    <HubSection Header="{Binding SearchHeader}" IsHeaderInteractive="True" > 
     <DataTemplate x:Name="SearchView"> 
      <local:SearchView/> 
     </DataTemplate> 
    </HubSection> 

    <HubSection Header="{Binding AddOfferHeader}" IsHeaderInteractive="True" > 
     <DataTemplate> 
      <local:AddOfferView/> 
     </DataTemplate> 
    </HubSection> 

    <HubSection Header="{Binding AccountHeader}" IsHeaderInteractive="True"> 
     <DataTemplate> 
      <local:AccountView/> 
     </DataTemplate> 
    </HubSection> 
</Hub> 

我都創造了的ViewModels,如NewestOffersViewModel,SearchViewModel等,但它沒有得到約束。我不忘記在容器配置中註冊它們。

@Edit: 用於容納所述輪轂的頁面視圖模型:

public class MainPageViewModel : PropertyChangedBase 
{ 
    public string HubHeader 
    { 
     get { return "Second Hand Bookshop"; } 
    } 
    public SearchViewModel SearchView { get; set; } 
    public NewestOffersViewModel NewestOffersViewModel { get; set; } 
    public MainPageViewModel() 
    { 
     SearchView = new SearchViewModel(); 
     NewestOffersViewModel = new NewestOffersViewModel(); 
    } 
    public string SearchSectionHeader 
    { 
     get { return "Search"; } 
    } 

而片MainPageView的(我試圖兵NewestOffersView到NewestOffersViewModel)。

<Hub Header="{Binding HubHeader}" > 
    <HubSection Header="{Binding NewestOffersHeader}" IsHeaderInteractive="True" > 
     <DataTemplate > 
      <ContentControl x:Name="NewestOffersViewModel" /> 
     </DataTemplate> 
    </HubSection> 

回答

2

好的,所以我得到了這個工作。 MainPageView:

<Hub Header="{Binding HubHeader}" > 
    <HubSection Header="{Binding NewestOffersHeader}" IsHeaderInteractive="True" > 
     <DataTemplate > 
      <local:NewestOffersView DataContext="{Binding NewestOffersViewModel}" /> 
     </DataTemplate> 
    </HubSection> 

    <HubSection Header="{Binding SearchHeader}" IsHeaderInteractive="True" > 
     <DataTemplate x:Name="SearchView"> 
      <local:SearchView DataContext="{Binding SearchViewModel}"/> 
     </DataTemplate> 
    </HubSection> 

    <HubSection Header="{Binding AddOfferHeader}" IsHeaderInteractive="True" > 
     <DataTemplate> 
      <local:AddOfferView DataContext="{Binding AddOfferViewModel}"/> 
     </DataTemplate> 
    </HubSection> 

    <HubSection Header="{Binding AccountHeader}" IsHeaderInteractive="True"> 
     <DataTemplate> 
      <local:AccountView DataContext="{Binding AccountViewModel}"/> 
     </DataTemplate> 
    </HubSection> 
</Hub> 

MainPageViewModel:

public class MainPageViewModel : PropertyChangedBase 
{ 
    public string HubHeader 
    { 
     get { return "Second Hand Bookshop"; } 
    } 
    public SearchViewModel SearchViewModel { get; set; } 
    public NewestOffersViewModel NewestOffersViewModel { get; set; } 
    public AddOfferViewModel AddOfferViewModel { get; set; } 
    public AccountViewModel AccountViewModel { get; set; } 
    public MainPageViewModel() 
    { 
     SearchViewModel = new SearchViewModel(); 
     NewestOffersViewModel = new NewestOffersViewModel(); 
     AddOfferViewModel = new AddOfferViewModel(); 
     AccountViewModel = new AccountViewModel(); 
    } 

現在看來很明顯,但前面我認爲,意見將得到解決相同的自動方式MainPageView。

@Henk Holterman說

使用在一個DataTemplate一個用戶控件的拋出過我一點點。

但我無法獲得帶有內容控制的版本,無法正常工作......對不起。 xd

相關問題