2
Answers

WPF: how to create a menu dinamically using MVVM

Photo of Angelo

Angelo

8y
492
1
Hi,
have someone a sample how to create a Menu with submenu, in a WPF C# project, using MVVM?

Answers (2)

1
Photo of Prakash Kumar
NA 1.6k 46.7k 8y
Refer this link,
 
http://www.c-sharpcorner.com/blogs/dynamic-menu-in-wpf122 
0
Photo of vipin kv
NA 200 0 8y
ViewModel
-------------------- 
public class MenuItemViewModel
{
public MenuItemViewModel()
{
this.MenuItems = new List<MenuItemViewModel>();
}
public string Text { get; set; }
public IList<MenuItemViewModel> MenuItems { get; private set; }
}
 
XAML Code
----------------------- 
<Window.Resources>
<HierarchicalDataTemplate DataType="{x:Type self:MenuItemViewModel}"
ItemsSource="{Binding Path=MenuItems}">
<ContentPresenter Content="{Binding Path=Text}" />
</HierarchicalDataTemplate>
</Window.Resources>
<DockPanel>
<Menu DockPanel.Dock="Top" ItemsSource="{Binding Path=MenuItems}" />
<Grid />
</DockPanel>