Click here to Skip to main content
13,259,194 members (50,178 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 27 Feb 2009

WPF PageFunction Object

, 27 Feb 2009
Rate this:
Please Sign up or sign in to vote.
This article demonstrates the use of the WPF PageFunction object in Page based WPF applications.


This article demonstrates the use of the WPF PageFunction object. The PageFunction object plays an important role in Page based WPF applications. The PageFunction class is almost similar to the Page class in WPF. You can design a PageFunction in XAML, and any WPF control can be added to the PageFunction object.


We will look at the WPF PageFunction object basics, like what the PageFunction object is, how the PageFunction object is different from the Page object, and how to make use of the PageFunction object.

The PageFunction class is a derived version of the Page class. Navigation service or hyperlinks can be used to navigate a PageFunction. The only thing that makes a PageFunction different from a Page object is it can return a value to the caller. This is like in Windows Forms applications where you show a dialog box to the user to collect some data and return back to the caller window. PageFunction can return any value, i.e., any .NET object like an integer, string or a custom object. By default, Visual Studio adds a String type as the return value like shown below, but you can change this to any .NET object you want:

<PageFunction xmlns="" 





 Title="PageFunction" RemoveFromJournal="True" />

Using the code

The sample project is really straightforward. The source contains a solution with a WPF project targeting the .NET Framework 3.5. The application has a Page (named “HomePage”) and a PageFunction (named “PageFunction”). The application is like a pizza-ordering system. The user navigates from the main menu (i.e., HomePage) to the selection menu (i.e., PageFunction) and allows the user to select toppings and return to the main menu with their selections.

When your PageFunction returns the values, you need to call the “OnReturn” method. The “OnReturn” method takes a type parameter specified for PageFunction. You can pass a null value as a parameter if you are not expecting any return value. In addition to this, the caller (the page that calls the PageFunction) has to handle the “Return” event for that PageFucntion, and an instance of the “ReturnEventAgrs” returned by that event will contain the returned value.

The home page calls the PageFunction on the click of a button, and at the same time, attaches the “Return” event handler to get the value returned by PageFunction. Shown below is a code example:

private void button1_Click(object sender, RoutedEventArgs e)
    //Create instance of PageFunction 
    PageFunction apage = new PageFunction();
    //Attach the Return EventHandler to Handle 
    //ReturnEventAgrs passed from PageFunction 
    apage.Return += new ReturnEventHandler<object>(apage_Return);
    //Navigate to Page Function 

/// <summary>
/// Return EventHanlder Method to ReturnEventAgrs passed from PageFunction
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void apage_Return(object sender, ReturnEventArgs<Object> e)
    //Display the Items selected
    List<string> selectedItems = (List<string>)e.Result;

    foreach (string item in selectedItems)

Now, let’s see how the PageFunction returns values to the caller. Once you are ready to return a value to the caller from a PageFunction, call the “OnReturn” method and pass the return value in a new instance of “ReturnEventArgs”. Shown below is a code example:

private void Button3_Click(object sender, RoutedEventArgs e)
    //Collect the selected items in List
    List<string> selectedItemsList = new List<string>();

    foreach (ListBoxItem listItem in listBox2.Items)

   //Create instance of ReturnEventArgs to pass data back to caller page
   ReturnEventArgs<object> returnObject = 
      new ReturnEventArgs<object>((object)selectedItemsList);

   //Call to PageFunction's OnReturn method and pass selected List

Other Important Properties of PageFunction:

  1. RemoveFromJournal”: When this property is set to true, it automatically deletes the entry from the Journal once your task is completed so the user won’t get a PageFunction during navigation.
  2. KeepAlive”: When this property is set to true, an instance of the page is retained in the navigation history.


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


About the Author

Saki Sachin
Software Developer (Senior)
United States United States
Sachin has been developing software professionally for more than five years. Mostly working with .NET Framework 3.5/3.0/2.0/1.x, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF)\Smart Client, ASP.NET 3.5/2.0/1.x (WebForm), XAML, ASP.NET AJAX 1.0, C# (3.0), LINQ, VB.NET, ADO.NET, WinForm, Web Services, MS Sync Framework, Window Services, UI Automation Framework, SQL Server and good experience in agile/scrum development methodology.

You may also be interested in...

Comments and Discussions

QuestionSeems convoluted. What are the advantages Pin
whiteheadw9-Jan-13 12:11
memberwhiteheadw9-Jan-13 12:11 
GeneralThanks Sachin Pin
vishalkumarsinha8-Jun-12 23:07
membervishalkumarsinha8-Jun-12 23:07 
GeneralThanks Sachin! Pin
achchu21-Oct-10 8:56
memberachchu21-Oct-10 8:56 

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 | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 27 Feb 2009
Article Copyright 2009 by Saki Sachin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid