How to add Menus at runtime in Main Window
i want after user login main menu is added . is it possible???? Please help me
The approach I would recommend is using a view-model where you define your LinkGroups and Links in an ObservableCollection. Your ModernWindow view should bind to this view-model. When your app is in a non-logged-in state, there are no groups and links, when logged-in you add your LinkGroups and Links. Makes sense?
Do you have any example for link-groups that can help me better? Thanks
Consider the following viewmodel
public class ViewModel
: NotifyPropertyChanged
{
private LinkGroupCollection groups = new LinkGroupCollection();
public LinkGroupCollection Groups
{
get { return this.groups; }
}
public void Login(string user, string password)
{
// TODO: validate credentials
var success = true;
if (success) {
// logged in, fill groups
var group = new LinkGroup { DisplayName = "group" };
group.Links.Add(new Link { DisplayName = "link 1" });
group.Links.Add(new Link { DisplayName = "link 2" });
this.groups.Add(group);
}
}
public void Logout()
{
// remove all groups and links
this.groups.Clear();
}
}
In your MainWindows.xaml.cs, assign the view model to the window DataContext like so;
public MainWindow()
{
InitializeComponent();
var vm = new ViewModel();
this.DataContext = vm;
// login
vm.Login(null, null);
}
And in you MainWindow.xaml, make sure the MenuLinkGroups are bound to the ViewModel.Groups;
<mui:ModernWindow x:Class="muitest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...
MenuLinkGroups="{Binding Groups}">
its working from mainwindow.xaml But i want to do it from login.xaml
is it possible or not just tell? can i add menus from different xaml page. I need your reply sir if its not possible i will think on any other solution? Your reply can save my time. Thanks
I think you can add an event or some kind of messaging pattern in this case, I've a small app that hides part of the menu when reaches some state, I used messaging to do this. see mvvm light (https://mvvmlight.codeplex.com/) for a sample
@heraclesaraujo can you say the files for this in your project? cause it's pretty big, and I don't want to take my day to search in it.
Place this is the login. It will access the menu items in the main window
var window = Application.Current.MainWindow as ModernWindow; window.MenuLinkGroups.Add(group);