Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:11:53+00:00 2026-06-04T06:11:53+00:00

This is more of a MVVM application design question. I’m trying to implement a

  • 0

This is more of a MVVM application design question. I’m trying to implement a very basic User-based permission management where the Visibility of certain controls in my application is derived from the Current User Role. Here my simplified model:

public class User
{
 public string UserName {get;set;}
 public Role UserRole {get;set;}
}

public enum Role
{
  Developer,
  BusinessAnalyst
}

public class MenuItemModel
{
   public ICommand Command {get;set;}
   public string Header {get;set;}
   public bool Visible {get;set;}
   private List<MenuItemModel> _Items;
   public List<MenuItemModel> Items
   {
        get { return _Items ?? (_Items = new List<MenuItemModel>()); }
        set
        {
            _Items = value;
        }
    }
}

My MainViewModel contains the following properties:

public class MainViewModel : ViewModelBase<MainViewModel>
{
    private ObservableCollection<MenuItemModel> _MainMenu;
    public ObservableCollection<MenuItemModel> MainMenu
    {
        get { return _MainMenu; }
        set
        {
            _MainMenu = value;
            NotifyPropertyChanged(x=>x.MainMenu);
        }
    }
    private User _CurrentUser;
    public User CurrentUser
    {
        get { return _CurrentUser; }
        set { 
            _CurrentUser = value;
            NotifyPropertyChanged(x=>x.CurrentUser);
        }
    }
}

Here my XAML where I declare and bind my Menu:

<Menu DockPanel.Dock="Top" ItemsSource="{Binding Path=MainMenu}">
            <Menu.ItemContainerStyle>
                <Style>
                    <Setter Property="MenuItem.Header" Value="{Binding Path=Header}"/>
                    <Setter Property="MenuItem.ItemsSource" Value="{Binding Path=Items}"/>
                    <!-- How to bind Visibility????-->
                    <!--Setter Property="MenuItem.Visibility" /-->
                </Style>
             </Menu.ItemContainerStyle>
         </Menu>

Now here my requirements:

  1. The Visibility of some UI controls (e.g. MenuItems) is dependent on User.Role. For example: MenuItemA should be visible to Role.Developer, but not for Role.BusinessAnalyst
  2. Some controls may be visible for more than one Role (e.g. Developer and Business Analyst)

My two options so far

  1. Create a custom Converter that has the logic to derive Visibility based on the Roles allowed and bind it to the MenuItem.Visibility Value property. The problem with this: a) The Roles allowed for this control need to be passed at Runtime since they come from the database and you cannot bind CommandParameters to a collection of Roles. b)How will the Converter have access to the User.Role to derive Visibility?
  2. Create the visibility logic inside a property in my UI model (e.g. MenuItemModel). But here I don’t want to create a dependency between my User and MenuItemModel classes.

What is the cleanest way to derive UI control Visibility dynamically based on User.Role without running into tightly-coupled scenarios (dependencies)?

Thanks!

Solution: So this is how I ended up solving it based on @fmunkert suggestion. Notice I had to change the MenuItemModel.Items property to List in order to access the ‘RemoveAll’ method:

    public MainViewModel()
    {
        //Create a new User with a Role
        InitializeUser();
        //Get all the Menus in the application
        List<MenuItemModel> allItems = GetAllMenus();
        //Remove recursively all Items that should not be visible for this user
        allItems.RemoveAll(x=>!IsVisibleToUser(x));
        //Set my MainMenu based on the filtered Menu list
        _MainMenu = new ObservableCollection<MenuItemModel>(allItems);
    }

    private void InitializeUser()
    {
        CurrentUser = new User {UserName = "apsolis", UserRole = Role.Developer};
    }

Here the method in my MainViewModel that removes forbidden Items recursively:

    /// <summary>
    /// Method to check if current MenuItem is visible to user
    /// and remove items that are forbidden to this user
    /// </summary>
    /// <param name="m"></param>
    /// <returns></returns>
    public bool IsVisibleToUser(MenuItemModel m)
    {
        if (m.Items != null && m.Items.Count > 0)
        {
            m.Items.RemoveAll(y=>!IsVisibleToUser(y));
        }
        return m.Roles == null || m.Roles.Contains(CurrentUser.UserRole);
    }

This seems to be working fine

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:11:54+00:00Added an answer on June 4, 2026 at 6:11 am

    Since you are generating the menu items in the ViewModel, I suggest not to use MenuItem.Visibility at all. Instead, have your ViewModel determine which menu items the user is allowed to see and fill the MainMenu collection only with that subset of menu items. I.e. your MainViewModel must know which menu items the user is allowed to see.

    I.e., you would use something like this in the constructor of your MainViewModel:

    _MainMenu = new ObservableCollection<MenuItemModel>(_allMenuItems.Select(m => IsVisibleToUser(m)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a MVVM question than a caliburn question, but it relates
I'm trying to implement a WPF application using MVVM (Model-View-ViewModel) pattern and I'd like
Ok, I'm going to try to make this more clear because my last question
This is more a general question but my particular case involves a ruby/rails app
This is more of a philosophical question than anything, so give me your thoughts.
(This is more of a curiousity question than any pending disaster :D ) So
This is more of a cryptography theory question, but is it possible that the
this is more of a subjective Question, but I'll ask it anyway. I'm about
I have a small WPF application based on MVVM priniciples. So far I had
I'm writing a WPF application while mostly adhering to the MVVM design pattern. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.