2015-06-18 23 views
0

我正在嘗試學習WPF,我創建了一個模板的資源字典用於我的Frame,但添加模板後Frame不再顯示我的Pages。當我刪除模板時,一切都會重新運行(頁面顯示正常)。我究竟做錯了什麼?添加模板後框架不再工作

ResourceDictionary.xaml(只是非常基本的)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

<ControlTemplate x:Key="frameTemplate" TargetType="{x:Type Frame}"> 
    <Grid> 
     <Border BorderBrush="Tomato" BorderThickness="3" Background="Bisque"/> 
    </Grid> 
</ControlTemplate> 

</ResourceDictionary> 

MainWindow.xaml

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/Resources/Icons.xaml" /> 
      <ResourceDictionary Source="ResourceDictionary.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 

<Controls:MetroWindow.RightWindowCommands> 
    <Controls:WindowCommands> 
     <ToggleButton Content="Menu" 
    IsChecked="{Binding ElementName=Flyout, Path=IsOpen}" Cursor="Hand"/> 
    </Controls:WindowCommands> 
</Controls:MetroWindow.RightWindowCommands> 

<Grid> 

    <Grid x:Name="menu_grid"> 
    </Grid> 

    <!-- flyout here, the title bar is not overlapped --> 
    <Controls:Flyout x:Name="Flyout" 
        Width="200" 
        Header="Menu" 
        IsOpen="True" 
        Position="Left"> 
     <StackPanel> 
      <Button HorizontalContentAlignment="Center" VerticalAlignment="Center" Margin="10" Click="DriverButton_Click"> 
       <StackPanel Orientation="Horizontal"> 
        <Rectangle Height="16" Width="16" Margin="5"> 
         <Rectangle.Fill> 
          <VisualBrush Visual="{StaticResource appbar_people}" Stretch="Fill" /> 
         </Rectangle.Fill> 
        </Rectangle> 
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">Drivers</TextBlock> 
       </StackPanel> 
      </Button> 
      <Button HorizontalContentAlignment="Center" VerticalAlignment="Center" Margin="10" Click="SeasonButton_Click"> 
       <StackPanel Orientation="Horizontal"> 
        <Rectangle Height="16" Width="16" Margin="5"> 
         <Rectangle.Fill> 
          <VisualBrush Visual="{StaticResource appbar_calendar}" Stretch="Fill" /> 
         </Rectangle.Fill> 
        </Rectangle> 
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">Seasons</TextBlock> 
       </StackPanel> 
      </Button> 
      <Button HorizontalContentAlignment="Center" VerticalAlignment="Center" Margin="10" Click="ConstructorsButton_Click"> 
       <StackPanel Orientation="Horizontal"> 
        <Rectangle Height="16" Width="16" Margin="5"> 
         <Rectangle.Fill> 
          <VisualBrush Visual="{StaticResource appbar_team}" Stretch="Fill" /> 
         </Rectangle.Fill> 
        </Rectangle> 
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">Constructors</TextBlock> 
       </StackPanel> 
      </Button> 
     </StackPanel> 
    </Controls:Flyout> 

    <Grid Margin="200 0 0 0"> 
     <Frame x:Name="_mainFrame" Template="{StaticResource frameTemplate}" />     
    </Grid> 

</Grid> 

MainWindow.xaml.cs

public partial class MainWindow 
{ 
    DriversPage driversPage = new DriversPage(); 
    SeasonPage seasonsPage = new SeasonPage(); 
    ConstructorsPage constructorsPage = new ConstructorsPage(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void DriverButton_Click(object sender, RoutedEventArgs e) 
    { 
     _mainFrame.Navigate(driversPage); 
    } 

    private void SeasonButton_Click(object sender, RoutedEventArgs e) 
    { 
     _mainFrame.Navigate(seasonsPage); 
    } 

    private void ConstructorsButton_Click(object sender, RoutedEventArgs e) 
    { 
     _mainFrame.Navigate(constructorsPage); 
    } 
} 

回答

1

當您覆蓋ControlTemplate時,您需要提供控件的整個模板。你只提供一個Grid,其中有一個Border,所以這就是所有要渲染的東西。如果您在Frameexample template,您會看到那裏有更多。我懷疑你需要顯示的頁面的重要部分可能是名爲"PART_FrameCP"的內容主持人。嘗試添加到您的模板。

<ContentPresenter x:Name="PART_FrameCP" /> 

命名部件通常在模板中很重要,因爲控件會查找它們。有時,未命名的部分也按類型搜索,因此可能也很重要。創建自己的模板時,閱讀和理解示例模板是個不錯的主意。

+0

添加內容展示器'PART_FrameCP'足以顯示頁面和邊框,我將在展開模板時查看您的鏈接示例。感謝您的回答。 – Sybren

相關問題