Click here to Skip to main content
13,740,815 members
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 25 May 2009
Licenced CPOL

WPF MasterPage

, 25 May 2009
Rate this:
Please Sign up or sign in to vote.
Create a MasterPage like functionality in WPF with 15 lines of code
WPF Masterpage Image


This article shows a way of creating MasterPage-like functionality in WPF with 15 lines of code.


One of the great things in .NET 2.0 was MasterPage. Unfortunately, MasterPage no longer exists in WPF. I have seen a lot of different approaches to achieving ASP.NET MasterPage-like functionality in WPF, with varying levels of complexity. I was not satisfied with any of the techniques and therefore came up with my own approach.


My solution is Frame. Frames can be used as a kind of content placeholder. "Ding". Surely that must ring a bell.

Using the Code

The sample code was written in C# using Visual Studio 2008 IDE. All the relevant code is in StartPage.xaml and should be self-explanatory. I will summarize the code shown below. You use a DockPanel for your MasterPage layouter. Then you add a first Frame and dock it to the top. Add a second Frame and dock it to the bottom, then add a StackPanel for your navigation (or whatever layout control you fancy) and dock it to the left or right. Finally add a Frame that will contain your content pages. The trick is to set the Source property of that Frame to a Page.

<Page ...>
    <DockPanel Name="pnlMaster" >
        <Frame Name="frmHeader" DockPanel.Dock="Top"
       Source="/MasterPages/HeaderPage.xaml" Height="100"></Frame>
        <Frame Name="frmFooter" DockPanel.Dock="Bottom"
        <StackPanel Name="spnlNavigator" DockPanel.Dock="Left"
       Width="150" Background="Orange">
            <Button Name="bntContentPage1" Margin="10,10,10,10"
       Content="Content Page 1" Click="OnClick"
       CommandParameter="ContentPage1.xaml" />

            <Button Name="bntContentPage2" Margin="10,10,10,10"
       Content="Content Page 2" Click="OnClick"
       CommandParameter="ContentPage2.xaml" />

         <Frame Name="frmMain" Source="/ContentPages/ContentPage1.xaml"></Frame>
        <![CDATA[<span class="code-SummaryComment">
         void OnClick(object sender, RoutedEventArgs e)
            frmMain.Source = new Uri("/ContentPages/" +
       ((Button)sender).CommandParameter.ToString(), UriKind.Relative);


  • 25th May, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) KALINK GmbH
Switzerland Switzerland
Pascal has been a passionate software developer since 1998. A native of Switzerland, he has worked around the world, from Ireland, England and Germany to as far afield as Australia and China.

Having started in C and moved on to C++, he now develops in C# on .NET and always does his best to keep up with new technologies such as WPF, WCF, WF, WebApi, Bootstrap, JavaScript and AngularJS.

He is interested in networking with like-minded software developers around the world.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Marco Bertschi29-Nov-12 22:50
memberMarco Bertschi29-Nov-12 22:50 
GeneralMy vote of 5 Pin
Member 805109012-Aug-11 3:20
memberMember 805109012-Aug-11 3:20 
Generalgood aticle Pin
Donsw14-Jun-09 7:16
memberDonsw14-Jun-09 7:16 
QuestionWhat about data binding? Pin
mattraffel2-Jun-09 4:00
membermattraffel2-Jun-09 4:00 
GeneralMy vote of 1 Pin
Member 159746326-May-09 1:22
memberMember 159746326-May-09 1:22 
GeneralRe: My vote of 1 Pin
pkaelin26-May-09 19:54
memberpkaelin26-May-09 19:54 
GeneralRe: My vote of 1 [modified] Pin
mattraffel1-Jun-09 15:45
membermattraffel1-Jun-09 15:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06-2016 | 2.8.180920.1 | Last Updated 25 May 2009
Article Copyright 2009 by pkaelin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid