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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:50:29+00:00 2026-06-06T04:50:29+00:00

I’m building an MVVM application in WPF and I am binding a Menu to

  • 0

I’m building an MVVM application in WPF and I am binding a Menu to a MenuItem model. My MenuItem class has these properties :

public class MenuItem
{
    private List<MenuItem> _Items;

    public MenuItem(string header, ICommand command)
    {
        Header = header;
        Command = command;
    }

    public MenuItem()
    {

    }

    public string Header { get; set; }

    public List<MenuItem> Items
    {
        get { return _Items ?? (_Items = new List<MenuItem>()); }
        set { _Items = value; }
    }

    public ICommand Command { get; set; }
    public string CommandName { get; set; }
    public object Icon { get; set; }
    public bool IsCheckable { get; set; }
    public bool IsChecked { get; set; }
    public bool Visible { get; set; }
    public bool IsSeparator { get; set; }
    public string ToolTip { get; set; }
    public int MenuHierarchyID { get; set; }
    public int ParentMenuHierarchyID { get; set; }
    public string IconPath { get; set; }
}

This MenuItem model class is populated from Data coming from a database. In this
case, the only property populated from DB is CommandName.

Let’s say it populates it with the string “OpenFile”

EDIT
Here my MenuViewModelConstructor:

    public MenuViewModel(MainViewModel _MainViewModel)
    {
       ....
    }

It has a dependency to MainViewModel because that’s where the OpenFile and CanOpenFile methods live.

My MenuViewModel has a method to register Commands as follows:

        private void RegisterMenuCommand(MenuItem item)
        {
            if(!string.IsNullOrEmpty(item.CommandName))
            {
                //How can I create a new RelayCommand instance from
                //my CommandName string???? 
                //e.g. item.Command = new RelayCommand(_MainViewModel.<item.CommandNameHere>, _MainViewModel."Can" + <item.CommandNameHere>
                item.Command = new RelayCommand(_MainViewModel.OpenFile, _MainViewModel.CanOpenFile);
            }

            foreach(MenuItem child in item.Items)
            {
                RegisterMenuCommand(child);
            }
        }

By the way, the signature of RelayCommand is:

public RelayCommand(Action execute, Func<bool> canExecute)

Is it possible to instantiate my RelayCommand with Reflection or dynamic lambdas or something like that so I can use my Command string coming from the database at runtime dynamically? What would be the most optimal way?

EDIT: SOLUTION
Thanks to @Nathan for pointing me to the right solution, here is my working method:

    private void RegisterMenuCommand(MenuItem item)
    {
        if(!string.IsNullOrEmpty(item.CommandName))
        {
            MethodInfo method1 = _MainViewModel.GetType().GetMethod(item.CommandName);
            Delegate d1 = Delegate.CreateDelegate(typeof(Action),_MainViewModel, method1);

            MethodInfo method2 = _MainViewModel.GetType().GetMethod("Can" + item.CommandName);
            Delegate d2 = Delegate.CreateDelegate(typeof (Func<bool>),_MainViewModel, method2);

            item.Command = new RelayCommand((Action)d1, (Func<bool>)d2);
        }

        foreach(MenuItem child in item.Items)
        {
            RegisterMenuCommand(child);
        }
    }

I am using .NET 4.0

Thanks!

  • 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-06T04:50:31+00:00Added an answer on June 6, 2026 at 4:50 am

    I did a quick google search on creating delegates with reflection and found this pretty good article How to: Hook Up a Delegate Using Reflection

    I created a quick test on my local machine and got it to work

    MethodInfo miHandler = typeof(MainWindow).GetMethod("OpenCommandHandler", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
    Delegate d = Delegate.CreateDelegate(typeof(Action<object>), this, miHandler);
    btnTest.Command = new DelegateCommand((Action<object>)d);
    

    where this in CreateDelegate is the view I was working from (MainWindow)

    You’ll have to tweak it a bit to get your’s to work but I imagine it would be something like:

    var obj = <object containing your method>
    
    MethodInfo miHandler = typeof(obj).GetMethod(item.CommandName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
    Delegate openDelegate = Delegate.CreateDelegate(typeof(Action), obj, miHandler);
    item.Command = new RelayCommand((Action)openDelegate, ...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

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.