Summary of the problem
Goal
My goal is to create a Navigation menu in WPF.
The navigation items are built in the View Model using Nodes.
Selected SubItem
of the menu will display the content of the selected Node.
Expected and actual result
This is what I have built so far:
Error
Now I need to present the selected MenuItem
to ContentPresenter
- and this is where I have the problem with.
What I have tried
This is my current code
XAML
<!--Menu-->
<Menu x:Name="NavigationTreeView" IsMainMenu="True" ItemsSource="{Binding Navigation}" Grid.Row="0">
<Menu.Resources>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Header" Value="{Binding Path=Title}" />
<Setter Property="Template" Value="{StaticResource VsMenuTop}" />
</Style>
<HierarchicalDataTemplate DataType="{x:Type nav:PageViewModel}" ItemsSource="{Binding Children}" />
</Menu.Resources>
</Menu>
<!--Content-->
<ContentPresenter Grid.Row="1" Content="{Binding ElementName=NavigationTreeView, Path=SelectedItem.Content}" />
This will not work, because I am not able to bind it to Menu using this line here: SelectedItem.Content
- and the Menu does not have a property SelectedItem
.
Alternative
My alternative option would be to use ListView
, because it contains a property SelectedItem
. But preferably I'd like to use Menu
.
Question
How can I get the selected item from the Hierarchical Data Template?