Reputation: 79
I'm building a prism application and I've setup the shell with a hamburgermenu from MahApps. Within the content of that Hamburgermenu I would like to have a region.
But when I try to make a region out of a ContentControl within "Hamburgermenu.Content" the region is not added. However, if I try to make a region out of a ContentControl OUTSIDE the Hamburgermenu-control, it works perfectly.
<Controls:MetroWindow x:Class="SystemCreator.ClientApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SystemCreator.ClientApplication"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:converters="http://metro.mahapps.com/winfx/xaml/shared"
xmlns:prism="http://prismlibrary.com/"
xmlns:inf="clr-namespace:SystemCreator.ClientApplication.Infrastructure;assembly=SystemCreator.ClientApplication.Infrastructure"
xmlns:cdviews="clr-namespace:SystemCreator.CreateDatabase;assembly=SystemCreator.CreateDatabase"
xmlns:test="clr-namespace:TestModule;assembly=TestModule"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="{Binding Title}" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Themes/HamburgerMenuTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="MahApps.Metro.Styles.HamburgerMenu" TargetType="{x:Type Controls:HamburgerMenu}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Focusable" Value="False" />
<Setter Property="HamburgerMenuTemplate">
<Setter.Value>
<DataTemplate>
<!-- PackIconMaterial - Menu -->
<ContentControl Width="22"
Height="22"
Content="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z"
Style="{DynamicResource PathIconContentControlStyle}" />
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="ItemContainerStyle" Value="{StaticResource HamburgerMenuItemStyle}" />
<Setter Property="KeyboardNavigation.ControlTabNavigation" Value="Local" />
<Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Local" />
<Setter Property="KeyboardNavigation.TabNavigation" Value="Local" />
<Setter Property="OptionsItemContainerStyle" Value="{StaticResource HamburgerMenuItemStyle}" />
<Setter Property="PaneBackground" Value="{DynamicResource MahApps.Metro.HamburgerMenu.PaneBackgroundBrush}" />
<Setter Property="PaneForeground" Value="{DynamicResource MahApps.Metro.HamburgerMenu.PaneForegroundBrush}" />
<Setter Property="Template" Value="{StaticResource HamburgerMenuTemplate}" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
<DataTemplate x:Key="MenuItemTemplate" DataType="{x:Type
Controls:HamburgerMenuIconItem}">
<Grid Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ContentControl Grid.Column="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{Binding Icon}"
Focusable="False"
IsTabStop="False" />
<TextBlock Grid.Column="1"
VerticalAlignment="Center"
FontSize="16"
Text="{Binding Label}" />
</Grid>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<Controls:HamburgerMenu x:Name="HamburgerMenuControl"
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="1"
HamburgerWidth="48"
DisplayMode="CompactInline"
VerticalScrollBarOnLeftSide="False"
ItemTemplate="{StaticResource MenuItemTemplate}"
OptionsItemTemplate="{StaticResource MenuItemTemplate}"
Style="{StaticResource MahApps.Metro.Styles.HamburgerMenu}"
Width="Auto"
>
<Controls:HamburgerMenu.HamburgerMenuHeaderTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="16"
Foreground="White"
Text="Menu" />
</DataTemplate>
</Controls:HamburgerMenu.HamburgerMenuHeaderTemplate>
<!--Content-->
<Controls:HamburgerMenu.Content>
<ContentControl prism:RegionManager.RegionName="{x:Static inf:ApplicationConstants.MainContent}" />
</Controls:HamburgerMenu.Content>
</Controls:HamburgerMenu>
</Grid>
</Controls:MetroWindow>
Since the region is not added while inside the Hamburgermenu, the navigation is not working. Do anyone have an idea on what i might have done wrong?
Upvotes: 3
Views: 1449
Reputation: 10863
The attached property (RegionManager.RegionName
) does only work for controls created immediately. Lazily created controls will not be detected, because the region manager is done looking for regions.
You need to add the region manually, in the menu's code behind (constructor), like this:
RegionManager.SetRegionName( theNameOfTheContentControlInsideTheMenu, WellKnownRegionNames.MenuRegion );
RegionManager.SetRegionManager( theNameOfTheContentControlInsideTheMenu, theRegionManager );
You'll have to assign a name to the content control hosting the region and somehow acquire the region manager (ServiceLocator.Current.GetInstance<IRegionManager>()
).
Upvotes: 2