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 9062009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:37:40+00:00 2026-06-16T15:37:40+00:00

MVVM is used. I created separate menu ‘Recent files’ which gets its items from

  • 0

MVVM is used. I created separate menu ‘Recent files’ which gets its items from binding. It looks like that:

enter image description here

        <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
        </MenuItem>

Now, I would like to add Command to each of the those auto-generated items, which should get the path as command parameter and execute import file action by click.

Could you please suggest how can it be done in MVVM way?

  • 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-16T15:37:41+00:00Added an answer on June 16, 2026 at 3:37 pm

    Again, found the solution by myself. I tried to put the command in wrong way like below, and it doesn’t work:

                <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
                <MenuItem.ItemContainerStyle>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="Command" Value="{Binding ImportRecentItemCommand}" />
                    </Style>
                </MenuItem.ItemContainerStyle>
            </MenuItem>
    

    Here is the right approach. Still don’t understand how it works, have to learn WPF deeply!

                <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
                <MenuItem.ItemContainerStyle>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="Command" Value="{Binding DataContext.ImportRecentItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}" />
                    </Style>
                </MenuItem.ItemContainerStyle>
            </MenuItem>
    

    EDIT: The final version

    XAML:

                <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
                <MenuItem.ItemContainerStyle>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="Command" Value="{Binding DataContext.ImportRecentItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}" />
                        <Setter Property="CommandParameter" Value="{Binding}" />
                    </Style>
                </MenuItem.ItemContainerStyle>
            </MenuItem>
    

    ViewModel: MVVM Light Toolkit is used, RelayCommand goes from there:

            private ICommand _importRecentItemCommand;
    
            public ICommand ImportRecentItemCommand
            {
                get { return _importRecentItemCommand ?? (_importRecentItemCommand = new RelayCommand<object>(ImportRecentItemCommandExecuted)); }
            }
    
            private void ImportRecentItemCommandExecuted(object parameter)
            {
                MessageBox.Show(parameter.ToString());
            }
    

    Enjoy

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

Sidebar

Related Questions

I'm building an MVVM application in WPF and I am binding a Menu to
I have multiple viewModels in my application and am binding/used them in ViewModelLocator mvvm
I've totally lost in the command binding that is used in MVVM. How should
In MVVM IDataErrorInfo is used for validation. In this on which time indexer check
I have been reading about MVP, MVVM and have used asp.net MVC in couple
In MVVM development I am constantly converting List<T> from my models to ObservableCollection<T> for
I'm learning MVVM. I have my View filling two comboboxes from ObservableCollection properties in
I have a simple MVVM application. It contains a property, which I will change
I have an application which I am developing using WPF\Prism\MVVM. All is going well
I've never used MVVM before, so I'm probably missing something obvious. When I create

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.