Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

NavigationService.FragmentNavigation Event

Occurs when navigation to a content fragment begins, which occurs immediately, if the desired fragment is in the current content, or after the source XAML content has been loaded, if the desired fragment is in different content.

Namespace: System.Windows.Navigation
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public event FragmentNavigationEventHandler FragmentNavigation
/** @event */
public void add_FragmentNavigation (FragmentNavigationEventHandler value)

/** @event */
public void remove_FragmentNavigation (FragmentNavigationEventHandler value)

In JScript, you can handle the events defined by a class, but you cannot define your own.
You cannot use this event in XAML.

By default, a content fragment is content that is contained by a named UIElement, which is a UIElement whose Name attribute is set, eg:

<TextBlock Name="FragmentName">...</TextBlock>

You navigate to a XAML fragment by providing a URI with a suffix in the following format:

#FragmentName

The following shows an example of a URI that refers to a content fragment:

#FragmentName

After the source XAML page loads (after LoadCompleted event is raised), fragment navigation begins and the NavigationService attempts to locate the XAML fragment. If the XAML fragment is found, NavigationService instructs the content navigator (NavigationWindow, Frame) to show the fragment. If you need to change this behavior, you can handle FragmentNavigation to provide your own fragment navigation behavior. FragmentNavigation is passed a FragmentNavigationEventArgs parameter which exposes properties that are useful for this purpose, including:

  • The navigator that owns this navigation service (NavigationWindow, Frame).

  • The fragment name.

You can handle FragmentNavigation to override the default WPF fragment implementation with your own custom implementation. If you do so, you need to set Handled to true; otherwise, the default WPF fragment processing behavior is applied.

NoteNote:

When NavigationService raises FragmentNavigation, it also raises Application.FragmentNavigation event on the Application object.

The following example shows how to handle FragmentNavigation to provide custom fragment navigation behavior. In this case, the example opens an error XAML page if the fragment in the source XAML page is not found.

void NavigationService_FragmentNavigation(object sender, FragmentNavigationEventArgs e)
{
    // Get content the ContentControl that contains the XAML page that was navigated to
    object content = ((ContentControl)e.Navigator).Content;

    // Find the fragment, which is the FrameworkElement with its Name attribute set
    FrameworkElement fragmentElement = LogicalTreeHelper.FindLogicalNode((DependencyObject)content, e.Fragment) as FrameworkElement;

    // If fragment found, bring it into view, or open an error page
    if (fragmentElement == null)
    {
        this.NavigationService.Navigate(new FragmentNotFoundPage());

        // Don't let NavigationService handle this event, since we just did
        e.Handled = true;
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

Show:
© 2014 Microsoft