2013-04-26 53 views
1

我有一個ContentControl,我想加載頁面myPage2。我的XAML Codefrom這個頁面看起來像這樣:將頁面加載到ContentControl中

<Page x:Class="ExampleApp.myPage2"> 
    <Grid x:Name="Content" Height="651" Width="941" Background="White"> 
     ... 
     ... 
    </Grid> 
</Page> 

我知道,我可以從一個頁面加載的資源與此代碼:

protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) 
{ 
    var contentControl = (ContentControl)container; 
    return (DataTemplate) contentControl.Resources[templateKey]; 
} 

我現在的問題是,我無法加載一個頁面像上面這樣的代碼。我寫這篇文章:

<Page x:Class="ExampleApp.myPage2"> 
    <Page.Resources> 
     <DataTemplate x:Key="Test">  
      <Grid x:Name="Content" Height="651" Width="941" Background="White"> 
       ... 
       ... 
      </Grid> 
     </DataTemplate> 
    </Page.Resources> 
</Page> 

,然後我可以從上面templateKey="Test"加載代碼相同的頁面。但主要問題是我想使用頁面的第一個聲明,而不想使用<Page.Resources> <DataTemplate x:Key="Test">等等。我想直接從第一個聲明中加載站點(本文後的第一個代碼)。如何直接從頁面創建DataTemplate?或者是否有其他方式將頁面加載到ContentControl中?

回答

1

沒有理由在ContentControl內使用Page。 A PageUserControl類的一個子類,它增加了支持在Frame控件中用於支持導航,反向堆棧/歷史記錄等的功能。您應該用XAML中的UserControl替代Page,並在後面添加代碼,以便最終獲得某些內容像這樣:

<UserControl x:Class="ExampleApp.myControl2"> 
    <Grid x:Name="Content" Height="651" Width="941" Background="White"> 
     ... 
     ... 
    </Grid> 
</UserControl> 

你可以把UserControl本身在DataTemplate,如果你想使用它作爲一個在DataTemplate一個ContentControl

<ContentControl 
    xmlns:controls="using:ExampleApp"> 
    <ContentControl.Resources> 
     <DataTemplate 
      x:Key="Test"> 
      <controls:myControl2 /> 
     </DataTemplate> 
    </ContentControl.Resources> 
</ContentControl> 
+0

大感謝你,完美的作品。我沒有關於UserControls - 真的很棒的解決方案。 – Cilenco 2013-04-26 18:26:25

相關問題