Click here to Skip to main content
15,942,847 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this class

internal class AppMenuItem
{
    public int AppMenuID { get; set; }
    public string ApphMenuName { get; set; }
    public string AppMenuUrlPath { get; set; }
    public int AppMenuParentID { get; set; }
    public List<AppMenuItem> AppSubMenuItems { get; set; }
}

In the MenuTable in the database, there are the same named columns.
The menu needs to have 1 level of parent items and under them child items.
I am using a menu control to display the menuitems on the WPF window

<Menu x:Name="AppMenu" Style="{DynamicResource ResourceKey=AppMainMenu}" ItemsSource="{Binding appMenuItemList}">
    <Menu.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>
    <DataTemplate>
        <MenuItem Header="{Binding AppMenuName}"/>
    </DataTemplate>
</Menu>



appMenuItemList is a List<appmenuitem> in which I am loading the data from the data table.

Is there an easier way to bind parent and child items to the menu control

What I have tried:

<Menu x:Name="AppMenu" Style="{DynamicResource ResourceKey=AppMainMenu}" ItemsSource="{Binding appMenuItemList}">
    <Menu.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>
    <DataTemplate>
        <MenuItem Header="{Binding AppMenuName}"/>
    </DataTemplate>
</Menu>
Posted

1 solution

The typical way to create a menu with sub-items is to use a template that looks something like this:
XML
<Menu ItemsSource="{Binding appMenuItemList}">
  <Menu.ItemContainerStyle>
    <Style TargetType="{x:Type MenuItem}">
        <Setter Property="Command" Value="{Binding Command}" />
    </Style>
  </Menu.ItemContainerStyle>
  <Menu.ItemTemplate>
    <HierarchicalDataTemplate DataType="{x:Type local:AppMenuItem}" ItemsSource="{Binding Path=AppSubMenuItems}">
        <TextBlock Text="{Binding AppMenuName}"/>
    </HierarchicalDataTemplate>
  </Menu.ItemTemplate>
</Menu>
Note that you have work to do here as you actually need to supply a command for the code to execute:
C#
internal class AppMenuItem
{
    public int AppMenuID { get; set; }
    public string AppMenuName { get; set; }
    public string AppMenuUrlPath { get; set; }
    public int AppMenuParentID { get; set; }
    public List<AppMenuItem> AppSubMenuItems { get; set; }
    public ICommand Command { get; set; }
}
I don't know what you're trying to do there so I can't supply a command for you. I haven't supplied code for your command implementation - you can find any number of ICommand implementations; search for RelayCommand for a good example to use as the source of your ICommand if you don't have one.
 
Share this answer
 
Comments
Christopher Fernandes 21-Jun-24 7:00am    
What I am unable to do is populate the parent & child items into List<appmenuitem> appMenuItemList using LINQ
Pete O'Hanlon 24-Jun-24 8:32am    
Without knowing what the data underneath looks like, I can't really help there. Bear in mind that's a different question to the one that you actually asked.
Christopher Fernandes 25-Jun-24 5:56am    
Data structure is same as the AppMenuItem class

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900