I have a ControlTemplate for a Menu which I want to be used for all my Pages. The function calls of the ControlTemplate requires the usage of the NavigationService belonging my NavigationWindow. However, I can not place the ControlTemplate in the NavigationWindow xaml file, because it is not accessible to the Pages. How would I allow my ControlTemplate to access the NavigationService while being available to use in my Pages?
My NavigationWindow is located at MainWindow.xaml
App.xaml.cs
namespace WPFApplication
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private void Menu_ModeAdvanced_Click(object sender, RoutedEventArgs e)
{
// Where I want to access the Navigation Service
}
}
}
App.xaml
<Application x:Class="WPFApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFApplication"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<!-- Page Design -->
<Style TargetType="Page">
<Setter Property="Foreground" Value="#FBF5F3" />
<Setter Property="Background" Value="#363537" />
</Style>
<!-- Label Design-->
<Style TargetType="Label">
<Setter Property="Foreground" Value="#FBF5F3" />
</Style>
</ResourceDictionary>
<!-- Menu Template-->
<ControlTemplate x:Key="StandardMenu" TargetType="Menu">
<Menu Height="20">
<MenuItem Header="File" AllowDrop="True">
<MenuItem Header="Mode" AllowDrop="True">
<MenuItem Header="Advanced"/>
</MenuItem>
</MenuItem>
</Menu>
</ControlTemplate>
</Application.Resources>
</Application>
I have tried putting the ControlTemplate and related code into a ResourceDictionary located in the NavigationWindow's class. However, I could not use it as a static resource in my pages.
Simply get the parent
Windowof theMenuItem. If it is theNavigationWindowyou can use its API to execute navigation related tasks or use its associatedNavigationWindow.NavigationService:But I recommend defining routed commands in your
NavigationWindow.It's much cleaner from a design perspective as the navigation details are kept private to the navigation host:
MainWindow.xaml.cs
App.xaml
PageId.cs