mui icon indicating copy to clipboard operation
mui copied to clipboard

How to add Menus at runtime in Main Window

Open solka2050 opened this issue 10 years ago • 8 comments

i want after user login main menu is added . is it possible???? Please help me

solka2050 avatar Mar 05 '15 13:03 solka2050

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?

kozw avatar Mar 07 '15 20:03 kozw

Do you have any example for link-groups that can help me better? Thanks

solka2050 avatar Mar 08 '15 07:03 solka2050

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}">

kozw avatar Mar 08 '15 11:03 kozw

its working from mainwindow.xaml But i want to do it from login.xaml

solka2050 avatar Mar 08 '15 17:03 solka2050

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

solka2050 avatar Mar 10 '15 09:03 solka2050

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 avatar Mar 11 '15 12:03 heraclesaraujo

@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.

sylveon avatar Sep 27 '15 13:09 sylveon

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);

WilliamsKobie avatar Apr 29 '16 00:04 WilliamsKobie