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

  • SEARCH
  • Home
  • 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 258931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:13:18+00:00 2026-05-11T22:13:18+00:00

I want to build a simple application with the MVVM pattern. This application will

  • 0

I want to build a simple application with the MVVM pattern.

This application will have two main parts:

  • menu on top
  • content below

The navigation will be simple:

  • each menu item (e.g. “Manage Customers” or “View Reports”) will fill the content area with a new page that has some particular functionality

I have done this before with code behind where the code-behind event-handler for menu items had all pages loaded and the one that should be displayed was loaded in as a child of a StackPanel. This, however, will not work in MVVM since you don’t want to be manually filling a StackPanel but displaying e.g. a “PageItem” object with a DataTemplate, etc.

So those of you who have made a simple click-menu application like this with MVVM, what was your basic application structure? I’m thinking along these lines:

MainView.xaml:

<DockPanel LastChildFill="False">

    <Menu 
        ItemsSource="{Binding PageItemsMainMenu}" 
        ItemTemplate="{StaticResource MainMenuStyle}"/>

    <ContentControl 
        Content="{Binding SelectedPageItem}"/>        

</DockPanel>

where the Menu is filled with a collection of “PageItems” and the DataTemplate displays the Title of each “PageItem object” as the Header of each MenuItem.

And the ContentControl will be filled with a View/ViewModel pair which has full functionality, but am not sure on this.

  • 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-05-11T22:13:18+00:00Added an answer on May 11, 2026 at 10:13 pm

    First, I think you should keep the code-behind event handler, there’s no point in changing a simple 2 line event handler to a complex command driven monster for no practical reason (and don’t say testebility, this is the main menu, it will be tested every time you run the app).

    Now, if you do want to go the pure MVVM route, all you have to do it to make your menu fire a command, first, in some resource section add this style:

    <Style x:Key="MenuItemStyle" TargetType="MenuItem">
        <Setter Property="Command" 
                Value="{Binding DataContext.SwitchViewCommand,
                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}"/>
        <Setter Property="CommandParameter" 
                Value="{Binding}"/>
    </Style>
    

    This style will make the menu item fire a the SwitchViewCommand on the attached view model with the MenuItem’s DataContext as the command parameter.

    The actual view is the same as your code with an additional reference to that style as the ItemContainerStyle (so it applies to the menu item and not the content of the DataTemplate):

    <DockPanel LastChildFill="False">
    
        <Menu DockPanel.Dock="Top"
            ItemsSource="{Binding PageItemsMainMenu}" 
            ItemTemplate="{StaticResource MainMenuStyle}"
            ItemContainerStyle="{StaticResource MenuItemStyle}"/>
        <ContentControl 
        Content="{Binding SelectedPageItem}"/>
    </DockPanel>
    

    Now in the view model you need (I used strings because I don’t have your PageItem code):

    private string _selectedViewItem;
    public List<string> PageItemsMainMenu { get; set; }
    public string SelectedPageItem
    {
        get { return _selectedViewItem; }
        set { _selectedViewItem = value; OnNotifyPropertyChanged("SelectedPageItem"); }
    }
    public ICommand SwitchViewCommand { get; set; }
    

    And use whatever command class you use to make the command call this code:

    private void DoSwitchViewCommand(object parameter)
    {
        SelectedPageItem = (string)parameter;
    }
    

    Now, when the user clicks a menu item the menu item will call the SwitchViewCommand with the page item as the parameter.

    The command will call the DoSwitchViewCommand that will set the SelectedPageItem property

    The property will raise the NotifyPropertyChanged that will make the UI update via data binding.

    Or, you can write a 2 line event handler, your choice

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From a quick look, it looks like the isValidLocation(mover,sx,sy,xp,yp) method… May 12, 2026 at 12:29 am
  • Editorial Team
    Editorial Team added an answer Take a look at Exiv2Sharp - this wrapper around Exiv2… May 12, 2026 at 12:29 am
  • Editorial Team
    Editorial Team added an answer Assuming that Tas in code #1 is Moveable in code… May 12, 2026 at 12:29 am

Related Questions

Background: I'm creating a WPF app using MVVM, and using a DI container to
I'm really disappointed with just about everything I find online that tries to teach
I'm in the process of building my first real WPF application (i.e., the first
I know this is a probable open ended question, and I have tried looking

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.