Sybren
Sybren

Reputation: 1079

Frame doesn't work anymore after adding template

I'm trying to learn WPF, I created a resource dictionary with a template for my Frame but after adding the template the Frame doesn't display my Pages anymore. When I remove the template, everything works again (the pages are properly displayed). What am I doing wrong?

ResourceDictionary.xaml (just very basic)

<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);
    }
}

Upvotes: 0

Views: 66

Answers (1)

Xavier
Xavier

Reputation: 3424

When you override a ControlTemplate, you need to supply the entire template for the control. You are only supplying a Grid with a Border in it, so that is all that is going to render. If you look at the example template for Frame, you will see there is a lot more there. I suspect the important piece that you need to display the page is probably the content presenter named "PART_FrameCP". Try adding that to your template.

<ContentPresenter x:Name="PART_FrameCP" />

Named parts are usually important in templates because the control will look for them. Sometimes, unnamed parts are also searched for by type and therefore may be important as well. It is a good idea to read and understand the example template when creating a template of your own.

Upvotes: 1

Related Questions