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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:21:24+00:00 2026-06-03T08:21:24+00:00

I have this context menu resource: <ResourceDictionary xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml> <ContextMenu x:Key=FooContextMenu> <ContextMenu.CommandBindings> <CommandBinding Command=Help

  • 0

I have this context menu resource:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ContextMenu x:Key="FooContextMenu">
        <ContextMenu.CommandBindings>
            <CommandBinding Command="Help" Executed="{Binding ElementName=MainTabs, Path=HelpExecuted}" />
        </ContextMenu.CommandBindings>

        <MenuItem Command="Help">
            <MenuItem.Icon>
                <Image Source="../Resources/Icons/Help.png" Stretch="None" />
            </MenuItem.Icon>
        </MenuItem>
    </ContextMenu>
</ResourceDictionary>

I want to re-use it in two places. Firstly I’m trying to put it in a DataGrid:

<DataGrid ContextMenu="{DynamicResource FooContextMenu}">...

The ContextMenu itself works fine, but with the Executed="..." I have right now breaks the application and throws:

A first chance exception of type ‘System.InvalidCastException’
occurred in PresentationFramework.dll

Additional information: Unable to cast object of type
‘System.Reflection.RuntimeEventInfo’ to type
‘System.Reflection.MethodInfo’.

If I remove the entire Executed="..." definition, then the code works (and the command does nothing/grayed out). The exception is thrown as soon as I right click the grid/open the context menu.

The DataGrid is placed under a few elements, but eventually they all are below a TabControl (called MainTabs) which has ItemsSource set to a collection of FooViewModels, and in that FooViewModel I have a method HelpExecuted which I want to be called.

Let’s visualize:

  • TabControl (ItemsSource=ObservableCollection<FooViewModel>, x:Name=MainTabs)
    • Grid
      • More UI
        • DataGrid (with context menu set)

Why am I getting this error and how can I make the context menu command to “target” the FooViewModel‘s HelpExecuted method?

  • 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-03T08:21:26+00:00Added an answer on June 3, 2026 at 8:21 am

    Unfortunately you cannot bind Executed for a ContextMenu as it is an event. An additional problem is that the ContextMenu does not exist in the VisualTree the rest of your application exists. There are solutions for both of this problems.

    First of all you can use the Tag property of the parent control of the ContextMenu to pass-through the DataContext of your application. Then you can use an DelegateCommand for your CommandBinding and there you go. Here’s a small sample showing View, ViewModel and the DelegateCommand implementation you would have to add to you project.

    DelegateCommand.cs

    public class DelegateCommand : ICommand
    {
        private readonly Action<object> execute;
        private readonly Predicate<object> canExecute;
    
        public DelegateCommand(Action<object> execute)
            : this(execute, null)
        { }
    
        public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
    
            this.execute = execute;
            this.canExecute = canExecute;
        }
    
        #region ICommand Members
    
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return canExecute == null ? true : canExecute(parameter);
        }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public void Execute(object parameter)
        {
            execute(parameter);
        }
    
        #endregion
    }
    

    MainWindowView.xaml

    <Window x:Class="Application.MainWindowView"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindowView" Height="300" Width="300"
            x:Name="MainWindow">
        <Window.Resources>
            <ResourceDictionary>
                <ContextMenu x:Key="FooContextMenu">
                    <MenuItem Header="Help" Command="{Binding PlacementTarget.Tag.HelpExecuted, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
                </ContextMenu>
            </ResourceDictionary>
        </Window.Resources>
        <Grid>
            <TabControl ItemsSource="{Binding FooViewModels}" x:Name="MainTabs">
                <TabControl.ContentTemplate>
                    <DataTemplate>
                        <DataGrid ContextMenu="{DynamicResource FooContextMenu}" Tag="{Binding}" />
                    </DataTemplate>
                </TabControl.ContentTemplate>
            </TabControl>
        </Grid>
    </Window>
    

    MainWindowView.xaml.cs

    public partial class MainWindowView : Window
    {
        public MainWindowView()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
        }
    }
    

    MainWindowViewModel.cs

    public class MainWindowViewModel
    {
        public ObservableCollection<FooViewModel> FooViewModels { get; set; }
    
        public MainWindowViewModel()
        {
            FooViewModels = new ObservableCollection<FooViewModel>();
        }
    }
    

    FooViewModel.cs

    public class FooViewModel
    {
        public ICommand HelpExecuted { get; set; }
    
        public FooViewModel()
        {
            HelpExecuted = new DelegateCommand(ShowHelp);
        }
    
        private void ShowHelp(object obj)
        {
            // Yay!
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a resource dictionary inside which I have a context menu: <ResourceDictionary x:Class=MyApp.Components.MyContextMenu
I have this need to place my app in the share/send context menu so
I read http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html but have some issues. For pre-honeycomb I want a custom title,
I have a context menu situated inside a User Control Resource. <UserControl.Resources> <ContextMenu x:Key=Menu1>
Making this tutorial: http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html I have ListActivity-derived class and onCreateContextMenu, onContextItemSelected overrides. I think
so I have this code: <toolbox id=navigator-toolbox> <toolbar id=abar accesskey=T class=chromeclass-toolbar context=toolbar-context-menu hidden=false persist=hidden>
I have a context menu that is defined as a resource and bound to
I have this menu structure that is used to navigate through content slider panels.
I have this var manager = context.ManagerInfoes.Select(m => m.Guid == managerGuid).First(); how can i
I have this reduce function: protected void reduce(Text key, Iterable<SortedMapWritable> values, Context context) throws

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.